Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. # 型定義ファイルのないモジュールを TypeScript で使用する
  2.  
  3. 方法がマジでわからない
  4.  
  5. 以下 TypeScript で `import foo from 'foo'` しようとしていて
  6. `types/foo/index.d.ts` に型定義ファイルを置いていると仮定する
  7.  
  8. ## `--paths` を使う方法
  9.  
  10. ```json
  11. {
  12. "compilerOptions": {
  13. "baseUrl": ".",
  14. "paths": {
  15. "*": ["types/*"]
  16. }
  17. }
  18. }
  19. ```
  20.  
  21. うまくいかない
  22.  
  23. ## `--typeRoots` を使う方法
  24.  
  25. ```json
  26. {
  27. "compilerOptions": {
  28. "typeRoots": {
  29. "*": [
  30. "node_modules/@types",
  31. "types"
  32. ]
  33. }
  34. }
  35. }
  36. ```
  37.  
  38. うまくいかない
  39.  
  40. `types/foo.d.ts` にしてもうまくいかない
  41.  
  42. ## node_modules/@types の下に配置する
  43.  
  44. これはうまくいく
  45.  
  46. `npm i` で配置されるようになるように、 `package.json` に以下のように書く
  47. ```json
  48. {
  49. "devDependencies": {
  50. "@types/foo": "file:types/foo",
  51. }
  52. }
  53. ```
  54.  
  55. `types/foo/package.json` に以下のように書く
  56. ```json
  57. {
  58. "name": "@types/foo",
  59. "types": "index.d.ts"
  60. }
  61. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement