From ecd3eeab2e1eb360557c32116aae2c29680affdc Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Tue, 15 Mar 2022 11:06:40 -0600 Subject: [PATCH] Improve support for backfilling types via .d.ts's 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 --- package.json | 2 +- src/dependencies.d.ts | 31 ------------------- tsconfig.build.json | 11 +++++++ tsconfig.json | 14 ++++----- types/@metamask/contract-metadata.d.ts | 1 + types/@metamask/metamask-eth-abis.d.ts | 1 + types/eth-ens-namehash.d.ts | 1 + .../src/createProvider.d.ts | 1 + types/eth-keyring-controller.d.ts | 1 + .../eth-phishing-detect/src/config.json.d.ts | 1 + types/eth-phishing-detect/src/detector.d.ts | 1 + types/eth-query.d.ts | 1 + types/ethjs-provider-http.d.ts | 1 + types/ethjs-unit.d.ts | 1 + types/isomorphic-fetch.d.ts | 1 + types/single-call-balance-checker-abi.d.ts | 1 + types/web3-provider-engine.d.ts | 1 + .../subproviders/provider.d.ts | 1 + types/web3-provider-engine/zero.d.ts | 1 + 19 files changed, 33 insertions(+), 40 deletions(-) delete mode 100644 src/dependencies.d.ts create mode 100644 tsconfig.build.json create mode 100644 types/@metamask/contract-metadata.d.ts create mode 100644 types/@metamask/metamask-eth-abis.d.ts create mode 100644 types/eth-ens-namehash.d.ts create mode 100644 types/eth-json-rpc-infura/src/createProvider.d.ts create mode 100644 types/eth-keyring-controller.d.ts create mode 100644 types/eth-phishing-detect/src/config.json.d.ts create mode 100644 types/eth-phishing-detect/src/detector.d.ts create mode 100644 types/eth-query.d.ts create mode 100644 types/ethjs-provider-http.d.ts create mode 100644 types/ethjs-unit.d.ts create mode 100644 types/isomorphic-fetch.d.ts create mode 100644 types/single-call-balance-checker-abi.d.ts create mode 100644 types/web3-provider-engine.d.ts create mode 100644 types/web3-provider-engine/subproviders/provider.d.ts create mode 100644 types/web3-provider-engine/zero.d.ts diff --git a/package.json b/package.json index 059084ba334..8840393358c 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", "test": "jest", "test:watch": "jest --watch", - "build": "rimraf dist && tsc --project .", + "build": "rimraf dist && tsc --project tsconfig.build.json", "build:watch": "yarn build --watch", "build:link": "yarn build && cd dist && yarn link && rm -rf node_modules && cd ..", "doc": "typedoc && touch docs/.nojekyll" diff --git a/src/dependencies.d.ts b/src/dependencies.d.ts deleted file mode 100644 index f94a6499a99..00000000000 --- a/src/dependencies.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -// TODO: Add types to each of these packages, replace them, or add full types here. - -declare module '@metamask/contract-metadata'; - -declare module '@metamask/metamask-eth-abis'; - -declare module 'eth-ens-namehash'; - -declare module 'eth-json-rpc-infura/src/createProvider'; - -declare module 'eth-keyring-controller'; - -declare module 'eth-phishing-detect/src/config.json'; - -declare module 'eth-phishing-detect/src/detector'; - -declare module 'eth-query'; - -declare module 'ethjs-provider-http'; - -declare module 'ethjs-unit'; - -declare module 'isomorphic-fetch'; - -declare module 'single-call-balance-checker-abi'; - -declare module 'web3-provider-engine'; - -declare module 'web3-provider-engine/subproviders/provider'; - -declare module 'web3-provider-engine/zero'; diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 00000000000..70df21e1651 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "./dist", + "outDir": "./dist", + "rootDir": "./src" + }, + "include": ["./src/**/*.ts"], + "exclude": ["**/*.test.ts"] +} diff --git a/tsconfig.json b/tsconfig.json index 8db604aaa42..2887202355a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,17 +1,15 @@ { "compilerOptions": { - "declaration": true, - "declarationDir": "dist", + "baseUrl": ".", "esModuleInterop": true, + "inlineSources": true, "module": "commonjs", "moduleResolution": "node", - "outDir": "dist", - "rootDir": "./src", + "paths": { + "*": ["./types/*"] + }, "sourceMap": true, - "inlineSources": true, "strict": true, "target": "es6" - }, - "include": ["src/**/*.ts"], - "exclude": ["**/*.test.ts"] + } } diff --git a/types/@metamask/contract-metadata.d.ts b/types/@metamask/contract-metadata.d.ts new file mode 100644 index 00000000000..dfbc40c8acb --- /dev/null +++ b/types/@metamask/contract-metadata.d.ts @@ -0,0 +1 @@ +declare module '@metamask/contract-metadata'; diff --git a/types/@metamask/metamask-eth-abis.d.ts b/types/@metamask/metamask-eth-abis.d.ts new file mode 100644 index 00000000000..d26af311c1b --- /dev/null +++ b/types/@metamask/metamask-eth-abis.d.ts @@ -0,0 +1 @@ +declare module '@metamask/metamask-eth-abis'; diff --git a/types/eth-ens-namehash.d.ts b/types/eth-ens-namehash.d.ts new file mode 100644 index 00000000000..1f686f57aea --- /dev/null +++ b/types/eth-ens-namehash.d.ts @@ -0,0 +1 @@ +declare module 'eth-ens-namehash'; diff --git a/types/eth-json-rpc-infura/src/createProvider.d.ts b/types/eth-json-rpc-infura/src/createProvider.d.ts new file mode 100644 index 00000000000..53b9352d5ce --- /dev/null +++ b/types/eth-json-rpc-infura/src/createProvider.d.ts @@ -0,0 +1 @@ +declare module 'eth-json-rpc-infura/src/createProvider'; diff --git a/types/eth-keyring-controller.d.ts b/types/eth-keyring-controller.d.ts new file mode 100644 index 00000000000..7576b89189c --- /dev/null +++ b/types/eth-keyring-controller.d.ts @@ -0,0 +1 @@ +declare module 'eth-keyring-controller'; diff --git a/types/eth-phishing-detect/src/config.json.d.ts b/types/eth-phishing-detect/src/config.json.d.ts new file mode 100644 index 00000000000..6943346451f --- /dev/null +++ b/types/eth-phishing-detect/src/config.json.d.ts @@ -0,0 +1 @@ +declare module 'eth-phishing-detect/src/config.json'; diff --git a/types/eth-phishing-detect/src/detector.d.ts b/types/eth-phishing-detect/src/detector.d.ts new file mode 100644 index 00000000000..cab272fdde9 --- /dev/null +++ b/types/eth-phishing-detect/src/detector.d.ts @@ -0,0 +1 @@ +declare module 'eth-phishing-detect/src/detector'; diff --git a/types/eth-query.d.ts b/types/eth-query.d.ts new file mode 100644 index 00000000000..e857105f282 --- /dev/null +++ b/types/eth-query.d.ts @@ -0,0 +1 @@ +declare module 'eth-query'; diff --git a/types/ethjs-provider-http.d.ts b/types/ethjs-provider-http.d.ts new file mode 100644 index 00000000000..6bee27c95f9 --- /dev/null +++ b/types/ethjs-provider-http.d.ts @@ -0,0 +1 @@ +declare module 'ethjs-provider-http'; diff --git a/types/ethjs-unit.d.ts b/types/ethjs-unit.d.ts new file mode 100644 index 00000000000..9030a9770f7 --- /dev/null +++ b/types/ethjs-unit.d.ts @@ -0,0 +1 @@ +declare module 'ethjs-unit'; diff --git a/types/isomorphic-fetch.d.ts b/types/isomorphic-fetch.d.ts new file mode 100644 index 00000000000..702932253d0 --- /dev/null +++ b/types/isomorphic-fetch.d.ts @@ -0,0 +1 @@ +declare module 'isomorphic-fetch'; diff --git a/types/single-call-balance-checker-abi.d.ts b/types/single-call-balance-checker-abi.d.ts new file mode 100644 index 00000000000..7bcda047d7d --- /dev/null +++ b/types/single-call-balance-checker-abi.d.ts @@ -0,0 +1 @@ +declare module 'single-call-balance-checker-abi'; diff --git a/types/web3-provider-engine.d.ts b/types/web3-provider-engine.d.ts new file mode 100644 index 00000000000..4d03b99ed28 --- /dev/null +++ b/types/web3-provider-engine.d.ts @@ -0,0 +1 @@ +declare module 'web3-provider-engine'; diff --git a/types/web3-provider-engine/subproviders/provider.d.ts b/types/web3-provider-engine/subproviders/provider.d.ts new file mode 100644 index 00000000000..1b953f44368 --- /dev/null +++ b/types/web3-provider-engine/subproviders/provider.d.ts @@ -0,0 +1 @@ +declare module 'web3-provider-engine/subproviders/provider'; diff --git a/types/web3-provider-engine/zero.d.ts b/types/web3-provider-engine/zero.d.ts new file mode 100644 index 00000000000..a1dcda9a562 --- /dev/null +++ b/types/web3-provider-engine/zero.d.ts @@ -0,0 +1 @@ +declare module 'web3-provider-engine/zero';