Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 型定義ファイルのないモジュールを TypeScript で使用する
- 方法がマジでわからない
- 以下 TypeScript で `import foo from 'foo'` しようとしていて
- `types/foo/index.d.ts` に型定義ファイルを置いていると仮定する
- ## `--paths` を使う方法
- ```json
- {
- "compilerOptions": {
- "baseUrl": ".",
- "paths": {
- "*": ["types/*"]
- }
- }
- }
- ```
- うまくいかない
- ## `--typeRoots` を使う方法
- ```json
- {
- "compilerOptions": {
- "typeRoots": {
- "*": [
- "node_modules/@types",
- "types"
- ]
- }
- }
- }
- ```
- うまくいかない
- `types/foo.d.ts` にしてもうまくいかない
- ## node_modules/@types の下に配置する
- これはうまくいく
- `npm i` で配置されるようになるように、 `package.json` に以下のように書く
- ```json
- {
- "devDependencies": {
- "@types/foo": "file:types/foo",
- }
- }
- ```
- `types/foo/package.json` に以下のように書く
- ```json
- {
- "name": "@types/foo",
- "types": "index.d.ts"
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement