-
-
Notifications
You must be signed in to change notification settings - Fork 268
Improve support for backfilling types via .d.ts's
#732
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
Conversation
75afffa to
e3a972e
Compare
|
Huh. I hadn't considered this before. We should make this update in our template repo as well. |
Gudahtt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I can't believe I hadn't noticed this before. Great PR description too, those repro steps were greatly appreciated
f4f0802 to
25ad0b9
Compare
Currently, we exclude test files from TypeScript's purview, so that only files in `src/` get emitted to `dist/`. This makes sense from a release perspective, but it interferes with development. One example of this interference involves [ambient modules][1]. We have a file `dependencies.d.ts` which we can use to backfill missing types for various dependencies. The problem is that while TypeScript (or, more accurately, `tsserver`) can see the types specified here when reading source files, it doesn't seem to pick them up for test files. This causes in-editor inconsistencies with type analysis and, in some cases, warnings such as: * `import` can only be used with `esInteropModuleFlag: true` * `X` is missing type definitions The reason this is happening is that type definition files are not intended to just be placed in the source directory. You can either use triple-slash directives to bring them in, or you can modify a setting in `tsconfig.json` (`paths`) that will [instruct TypeScript where it should find types for modules that are imported][1]. This commit chooses the latter option. Note that this requires we split up `dependencies.d.ts` into separate type definition files, one per module. In addition to modifying `tsconfig.json`, this commit also breaks up this config file into a global/development version and a version that is specific to the build/release process. This allows editors to use the same exact TypeScript settings for test files as for non-test files. `tsconfig.build.json` is used by `yarn build` and will limit the files emitted to `dist/` to only non-test files in `src/`, as in the original configuration. [1]: https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules [2]: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
d130fde to
ecd3eea
Compare
.d.ts's
|
Okay, I've created a similar PR in the template repo here: MetaMask/metamask-module-template#95. As I was doing this, I learned a few more things about type definition files, and updated this PR to match those findings. I've left some more commentary on that particular PR than here, so that might be informative if you're not sure why |
Gudahtt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Currently, we exclude test files from TypeScript's purview, so that only files in `src/` get emitted to `dist/`. This makes sense from a release perspective, but it interferes with development. One example of this interference involves [ambient modules][1]. We have a file `dependencies.d.ts` which we can use to backfill missing types for various dependencies. The problem is that while TypeScript (or, more accurately, `tsserver`) can see the types specified here when reading source files, it doesn't seem to pick them up for test files. This causes in-editor inconsistencies with type analysis and, in some cases, warnings such as: * `import` can only be used with `esInteropModuleFlag: true` * `X` is missing type definitions The reason this is happening is that type definition files are not intended to just be placed in the source directory. You can either use triple-slash directives to bring them in, or you can modify a setting in `tsconfig.json` (`paths`) that will [instruct TypeScript where it should find types for modules that are imported][1]. This commit chooses the latter option. Note that this requires we split up `dependencies.d.ts` into separate type definition files, one per module. In addition to modifying `tsconfig.json`, this commit also breaks up this config file into a global/development version and a version that is specific to the build/release process. This allows editors to use the same exact TypeScript settings for test files as for non-test files. `tsconfig.build.json` is used by `yarn build` and will limit the files emitted to `dist/` to only non-test files in `src/`, as in the original configuration. [1]: https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules [2]: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
Currently, we exclude test files from TypeScript's purview, so that only files in `src/` get emitted to `dist/`. This makes sense from a release perspective, but it interferes with development. One example of this interference involves [ambient modules][1]. We have a file `dependencies.d.ts` which we can use to backfill missing types for various dependencies. The problem is that while TypeScript (or, more accurately, `tsserver`) can see the types specified here when reading source files, it doesn't seem to pick them up for test files. This causes in-editor inconsistencies with type analysis and, in some cases, warnings such as: * `import` can only be used with `esInteropModuleFlag: true` * `X` is missing type definitions The reason this is happening is that type definition files are not intended to just be placed in the source directory. You can either use triple-slash directives to bring them in, or you can modify a setting in `tsconfig.json` (`paths`) that will [instruct TypeScript where it should find types for modules that are imported][1]. This commit chooses the latter option. Note that this requires we split up `dependencies.d.ts` into separate type definition files, one per module. In addition to modifying `tsconfig.json`, this commit also breaks up this config file into a global/development version and a version that is specific to the build/release process. This allows editors to use the same exact TypeScript settings for test files as for non-test files. `tsconfig.build.json` is used by `yarn build` and will limit the files emitted to `dist/` to only non-test files in `src/`, as in the original configuration. [1]: https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules [2]: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
Currently, we exclude test files from TypeScript's purview, so that only
files in
src/get emitted todist/. This makes sense from a releaseperspective, but it interferes with development.
One example of this interference involves ambient modules. We have
a file
dependencies.d.tswhich we can use to backfill missing typesfor various dependencies. The problem is that while TypeScript (or, more
accurately,
tsserver) can see the types specified here when readingsource files, it doesn't seem to pick them up for test files. This
causes in-editor inconsistencies with type analysis and, in some cases,
warnings such as:
importcan only be used withesInteropModuleFlag: trueXis missing type definitionsThe reason this is happening is that type definition files are not
intended to just be placed in the source directory. You can either use
triple-slash directives to bring them in, or you can modify a setting in
tsconfig.json(paths) that will instruct TypeScript where it shouldfind types for modules that are imported. This commit chooses the
latter option. Note that this requires we split up
dependencies.d.tsinto separate type definition files, one per module.
In addition to modifying
tsconfig.json, this commit also breaks upthis config file into a global/development version and a version that is
specific to the build/release process. This allows editors to use
the same exact TypeScript settings for test files as for non-test files.
tsconfig.build.jsonis used byyarn buildand will limit the filesemitted to
dist/to only non-test files insrc/, as in the originalconfiguration.
Manual testing steps
mainbranch.src/assets/AssetsContractController.test.ts.MAINNET_PROVIDERon line 6. Note how the type isany. This is because the types forethjs-provider-httpis being stubbed insrc/dependencies.d.ts.declare module 'ethjs-provider-http';line insrc/dependencies.d.tsto read:MAINNET_PROVIDERinsrc/assets/AssetsContractController.test.tsagain. Note how the type is stillany.src/dependencies.d.ts.src/dependencies.d.ts.MAINNET_PROVIDERinsrc/assets/AssetsContractController.test.tsa third time. Note how the type is properlyHttpProvider.yarn build. All of the files we would expect to show up indist/should be there (i.e. no extra.d.tsfiles, no tests, etc.)