Skip to content

Add TypeScript type checking - #6862

Merged
TheLarkInn merged 32 commits into
webpack:masterfrom
mohsen1:ts
Apr 12, 2018
Merged

TheLarkInn merged 32 commits into
webpack:masterfrom
mohsen1:ts

Conversation

@mohsen1

@mohsen1 mohsen1 commented Mar 25, 2018 •

Copy link
Copy Markdown
Contributor

Summary

Wanted to try using TypeScript with allowJs and checkJs to see if it can improve code correctness.

Webpack is a good case study for using TypeScript compiler on JavaScript projects. This is still a work in progress and I'm not sure if we can use TypeScript to check for errors without any code modifications.

Issues in TypeScript and DefiantlyTyped

Issues found in Webpack by TypeScript

When this is complete?

This is going to be a lot of work to get it to the finish line and I'm not sure if it is inline with the project goals. This is why I'm opening the pull request early. Hoping we can overcome all of those TypeScript issues and make use of TypeScript compiler in Webpack! 🙏

Only comment changes

This pull request on its own should not hav any code changes. TypeScript type checking should be non-invasive and should not make users to change their code. At most, changes should be comments only. If any code change is necessary due to bugs or TS limitations, we are going to propose and merge them in separate pull request.

TypeScript 2.9

The amazing TypeScript team have fixed many issues with the compiler when checking JavaScript code. Those changes are currently (April 11) in the nightly builds of version 2.9. We can merge while depending on nightly version but it would be nicer to depend to beta release in master. We should eventually depend on a stable release.

@TheLarkInn @DanielRosenwasser @mhegazy

@jsf-clabot

jsf-clabot commented Mar 25, 2018 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mhegazy

mhegazy commented Mar 25, 2018

Copy link
Copy Markdown

@mohsen1 the ‘_this’ error looks like a bug, mind filing this on the TS side.

@montogeek

Copy link
Copy Markdown
Contributor

This was discussed?

@mohsen1

mohsen1 commented Mar 25, 2018

Copy link
Copy Markdown
Contributor Author

@montogeek I don't believe it was discussed before. I'm using Webpack to test the idea of using TypeScript for type checking large JavaScript projects. Would love to finish it up and a complete case study.

Is this is something worth merging if I can complete it? I'm not sure! I would say yes but it's up to owners obviously.

@sokra

sokra commented Mar 26, 2018

Copy link
Copy Markdown
Member

This is interesting. I also wanted to try using typescript as linter but had no time for this yet.

Please fix any errors you find in separate PRs so we can merge them independently and this PR doesn't get filled up with fixes. Instead it should only contain the ts-as-linter stuff.

Error 1 seem to be really broken, Error 2 and 3 are also true but have no negative effect...

Comment thread package.json Outdated
@@ -103,6 +106,7 @@
"pretest": "npm run lint-files",
"lint-files": "npm run lint && npm run schema-lint",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

call the typescript linting here, to add it to the CI

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do once number of errors is reduced. The lint bot comments will pollute this thread if I do. See #6869

@sokra

sokra commented Mar 26, 2018 •

Copy link
Copy Markdown
Member

would it be possible to add (stricter) interfaces as additional .d.ts file? I. e. adding Module.d.ts with an interface for the Module.

I only want to know if this is possible. The first PR should only add the infrastructure.

@Jessidhia

Copy link
Copy Markdown

It is possible, but the type definitions there are not used to validate the contents of Module.js itself. It only affects other modules that import it.

@marvinhagemeister

Copy link
Copy Markdown

One could always add // @ts-check at the top of a ´js` file. See: http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-3.html

@Jessidhia

Jessidhia commented Mar 26, 2018 •

Copy link
Copy Markdown

You can, it's just that that still doesn't bring any relationship between the .js file and its own .d.ts declaration; you have to write all the JSDoc annotations in duplicate. It's not even possible to refer to types declared on your own .d.ts file unless it's a global type 😢

@sokra

sokra commented Mar 26, 2018

Copy link
Copy Markdown
Member

It's sad that it doesn't validate the content ifself, but we can still use it to describe the public interface of these classes.

@sandersn

Copy link
Copy Markdown
Contributor

@mohsen1 can you point me to examples like class Foo {}; let f = new Foo(); f.bar = 1 in the source? I was experimenting with webpack+checkjs a couple of weeks ago and missed this pattern. (You might consider filing on bug on Typescript for it too.)

@mohsen1

mohsen1 commented Mar 26, 2018

Copy link
Copy Markdown
Contributor Author

@sokra see #6869

I got lazy and accepted a lot of TypeScript "Quick FIx" suggestions so its not the best change list to fix all issues but there are some interesting things in that PR!

if you're onboard with enabling TS as a linter in Webpack I can take a more incremental approach and make smaller PRs against this branch and go over issues one by one.

@mohsen1

mohsen1 commented Mar 27, 2018

Copy link
Copy Markdown
Contributor Author

@sandersn filed a ticket for that issue as well
microsoft/TypeScript#22896

@sokra sokra left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is ready now.

Let's see if the CI passes.

@sokra
sokra requested a review from ooflorent April 12, 2018 09:53
@sokra sokra changed the title (WIP) Add TypeScript type checking Add TypeScript type checking Apr 12, 2018
@sokra

sokra commented Apr 12, 2018

Copy link
Copy Markdown
Member

Future PRs could now add types to stuff by using jsdoc. I guess it would be useful to have types on the basic types.

Comment thread tsconfig.json
@@ -0,0 +1,62 @@
{
"compilerOptions": {
/* Basic Options */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we get rid of the comments?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's a JSON5 file, but can't use .json5 🤷‍♀️

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's an issue with Github. Commented JSON is fine. Even Mr @douglascrockford is cool with it ;)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I do not know what JSON5 is, but whatever it is, I am not cool with it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

oh my, way to go on using the ad verecundiam in public @mohsen1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@douglascrockford Re I do not know what JSON5 is:

The JSON5 Data Interchange Format (JSON5) is a superset of JSON that aims to alleviate some of the limitations of JSON by expanding its syntax to include some productions from ECMAScript 5.1.

@webpack-bot

Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

Comment thread lib/NormalModule.js
this.errors.push(new ModuleError(this, error));
},
exec: (code, filename) => {
// @ts-ignore Argument of type 'this' is not assignable to parameter of type 'Module'.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

normal module is not precisely implementing native module. This is why ts ignore directive is here.

We can use jsdoc @implements to actually resolve it but then this class needs some additional members to satisfy normal module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should not block merging this pull request though..

@webpack-bot

Copy link
Copy Markdown
Contributor

It looks like this Pull Request doesn't include enough test cases (based on Code Coverage analysis of the PR diff).

A PR need to be covered by tests if you add a new feature (we want to make sure that your feature is working) or if you fix a bug (we want to make sure that we don't run into a regression in future).

@mohsen1 Please check if this is appliable to your PR and if you can add more test cases.

Read the test readme for details how to write test cases.

@mohsen1

mohsen1 commented Apr 12, 2018

Copy link
Copy Markdown
Contributor Author

@sokra Thanks for fixing those issues. Next big task is to add complete JSDoc coverage. Once we have that we can enable the strict mode.

@TheLarkInn We'll need community support to add JSDoc coverage. It would be nice if you can do what you did for converting to ES2015 by involving the community. It's lots of work and we need help!

Lets merge this! 🎉

@sandersn

Copy link
Copy Markdown
Contributor

@mohsen1 note that you can turn on strictNullChecks without any of the other strict flags. In Typescript's test that compiles the npm-distributed version of webpack, we only see 28 "Object is possibly undefined errors".

@TheLarkInn

TheLarkInn commented Apr 12, 2018 •

Copy link
Copy Markdown
Member

@mohsen1 thank you so much for this. Let's land it.

@TheLarkInn
TheLarkInn merged commit 10282ea into webpack:master Apr 12, 2018
@TheLarkInn

Copy link
Copy Markdown
Member

@mohsen1 I think it would make sense to create issues that would involve turning each of the strict checks on and implementing the coverage required for them now and contributors can tackle these bit by bit.

@johnnyreilly

Copy link
Copy Markdown
Contributor

Well done @mohsen1!!!

@rodoabad

Copy link
Copy Markdown

Good job! ❤️

@davidm-public

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.