Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeScript types #1132

Merged
merged 7 commits into from Jan 11, 2019
Merged

Conversation

wingrunr21
Copy link
Contributor

This resolves #1108

  • Adds types property to package.json (see Publishing docs)
  • Revamps the typings.d.ts file to more accurately reflect both the internal and external API. Specifically, the Options interface should have every property marked optional (for external consumption) but most of those properties are assumed to be not null/undefined throughout index.js. I thus introduced an InternalOptions interface which matches this requirement.
  • Moved the Hooks typedef into the typings.d.ts file vs defining it inline.
  • Moved HtmlTagObject inside the HtmlWebpackPlugin namespace.

I did not fix compatibility with noImplicitAny in this PR. As you are shipping an annotated JS file, I believe TS users downstream with both noImplicitAny and allowJs enabled will get a bunch of compiler warnings.

cc @SimonSchick

Copy link
Owner

@jantimon jantimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like what you did here!

Unfortunately sorting the options made it very hard to review this pull request.
Could you please add some changes like described above?

index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
lib/hooks.js Show resolved Hide resolved
typings.d.ts Outdated Show resolved Hide resolved
@wingrunr21
Copy link
Contributor Author

Sorry about the Options sorting thing. Should have dropped that in an independent commit. I'll push some changes!

@jantimon jantimon merged commit a524e8f into jantimon:master Jan 11, 2019
@jantimon
Copy link
Owner

Just realized that the Require<Omit does not work. All properties are still optional.

@wingrunr21
Copy link
Contributor Author

Hmm, I tested it locally. Omit is for sure working. Let me look at it.

@wingrunr21
Copy link
Contributor Author

Ok, scratch that. Looks like Omit is the one causing the problem. That's odd as that's the official definition of the type (see here). I'll keep looking.

@wingrunr21
Copy link
Contributor Author

Ok, so apparently I didn't need to redefine Required as in TS 2.8+ it is part of lib.d.ts.

The other part is apparently you can remove the optionality aspect of a type but you can't remove its ability to be set to undefined (see here).

So, this approach may not actually solve the use case I was trying to solve. As it's still internal it may not be that big of a deal but...

@jantimon
Copy link
Owner

Well right now the types were used to guarantee the quality and to reduce bugs..
Why would that be not a big deal?

@wingrunr21
Copy link
Contributor Author

Because they weren't accurate to begin with. If you're that worried about it feel free to revert this PR. Your TS support for downstream users is completely broken though. How are they supposed to guarantee quality and reduce bugs?

@jantimon
Copy link
Owner

jantimon commented Jan 12, 2019

They were very accurate for all internal flows and there was @types/html-webpack-plugin for downstream users.

If an internal function relies on values which are not present it might cause bugs in edge case situations

What was not accurate?

@wingrunr21
Copy link
Contributor Author

wingrunr21 commented Jan 12, 2019

I'm not going to debate this, sorry. I think #1108 + the explanations in this PR cover the reasons.

So, looks like I spoke too soon. I poked at this some more and the problem is actually the wildcard [option: string]: any line at the bottom of the Options interface. If you comment that out then the Required<Omit stuff works the way it is supposed to.

You can see a simple example here on the TS playground. Comment out line 4 and you can see the errors change accordingly.

@jantimon
Copy link
Owner

I guess the cleanest way would be to write two interfaces or to start with the ProcessedOption and use Partial again

@jantimon
Copy link
Owner

Before we relied on the webpack source code - now we rely on @types/webpack

@wingrunr21
Copy link
Contributor Author

Before we relied on the webpack source code - now we rely on @types/webpack

No it doesn't. See https://github.com/jantimon/html-webpack-plugin/blob/master/index.js#L7-L8

@jantimon
Copy link
Owner

But why did we have to add @types/webpack ?

@wingrunr21
Copy link
Contributor Author

@jantimon
Copy link
Owner

Thanks good hint :)

I’ll try to refactor those changes on a new or tomorrow

@lock lock bot locked as resolved and limited conversation to collaborators Feb 11, 2019
@jantimon
Copy link
Owner

@wingrunr21 I fixed the remaining open points - would love to get your feedback:

f4cb241

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missings typings property in package.json in beta
2 participants