[POC] Incremental switch to Typescript#9517
Conversation
@rosskevin Maybe we should remove |
|
I put back those files to prevent the diff. You can ignore changes in the |
|
Wow, looks like this is really happening?! 🙃 Thanks @rosskevin for starting the effort. As @oliviertassinari already mentioned removing the I'll will have more time on Monday to take a look. Regarding your "sticky transition points":
|
|
@sebald the |
|
@sebald these are the errors that require the import workaround: I tried going back on deep merge to the |
|
I'm continuing some conversion to ts of internal code on a branch - trying to inch forward to something mergeable. When all of you are done with review, I can merge my other branch to this PR. |
| import classNames from 'classnames'; | ||
| import warning from 'warning'; | ||
| import * as React from 'react'; | ||
| import * as PropTypes from 'prop-types'; |
There was a problem hiding this comment.
It seems required for React: https://github.com/react-toolbox/react-toolbox/blob/agnostic-typescript/packages/react-toolbox-core/src/components/Dialog/Dialog.tsx#L1 But why do we need this change for the other imports?
There was a problem hiding this comment.
Most imports from non-ES2015+ modules are effectively import namespace as, which means the module's exports plus types (if any) - import them as a single named variable. So module.exports = warning is both not a named export AND not a default export, which requires this syntax to effectively get all exports as warning.
Notice that our own imports with default exports need none of this, so as we eventually graduate to ES2015 modules we won't need these even with external modules, unless we choose to import all their types as well with that variable.
Effectively - this is valid spec javascript.
There was a problem hiding this comment.
Thanks for the explanation.
There was a problem hiding this comment.
Well, we have the allowSyntheticDefaultImports flag, that can make things like "import React from 'react'" valid
see: https://www.typescriptlang.org/docs/handbook/compiler-options.html
and: https://javascriptplayground.com/blog/2017/04/react-typescript/
I think that we need to be conservative and initially just change only the syntax that are really invalid
There was a problem hiding this comment.
The allowSyntheticDefaultImports flag doesn't actually change the semantics of the generated code, it just disables the error AFAIK, so this is not a good thing for libraries to do IMO. Webpack may compensate for it, but your tests running in node would fail. There's a feature coming in TypeScript 2.7 which will bring the semantics in line with Babel's however.
There was a problem hiding this comment.
That has also been my experience @pelotom. Turning it on just helps find the errors quicker.
|
I've done the |
|
Question, does it make sense to try to convert one component here too? |
|
I can, but no it won't matter. I'm going to work on my continued branch to see if I can solve all TS errors and make tests run. |
| "jsx": "react", | ||
| "moduleResolution": "node", | ||
| "lib": [ | ||
| "es2017", |
There was a problem hiding this comment.
Which es2017 features are required by material-ui out of curiosity? That seems like a strong requirement to impose on downstream users.
There was a problem hiding this comment.
I'm not sure, we can reel that back in when tests run.
|
I've got all TS errors fixed, working on tests. If everyone is good with this, I'll merge that work here. Just be aware that the diff is mostly useless. I can also just leave this PR as-is, close it and open a new one. |
|
PR #9535 is up, and it's very close to mergeable. |
The goal
The goal of this PR is to as minimally as possible get to the typescript tool chain to allow us to incrementally convert to typescript.
This POC (as-is) proves
Paper.spec.tsxSticky transition points
module.exports = warningare different in TS, they need to useimport deepmerge = require('deepmerge')and this workaround can only be applied in a.tsfile. In an effort to minimize this POC and keep the filesjsfor the moment - I created a folder/utils/ts-import-workaroundwith relevant re-exports. As soon as the files are moved from js to ts, this is totally unnecessaryeslint-disable-lineon the above imports so that eslint is happy with the js. As (or if) we move to TS, we should consider moving to tslint instead of eslint. Smarter type aware linting with much of the same rules are available, though not as extensive as eslint. There are more options hereAdvantages
In the minimized amount of work during this process I uncovered several TS definition bugs in our code. These were trivial but nonetheless - doing something like this will firm up our use of TS definitions and/or ts files themselves. And, of course, this also gives us a way to move to fully typed TS at our own pace.
Next steps
If we decide to move ahead, move through the rest of the typescript errors in the project which are:
When removing flow on utility files, it might be just as easy to swap them to ts at that point and convert the flow types.