代码之家  ›  专栏  ›  技术社区  ›  Konda

混合项目中.js文件的Typescript声明

  •  0
  • Konda  · 技术社区  · 7 年前

    嗨,我正在把我的js react redux项目转换成typescript。我想从我的redux reducers文件开始。

    Project/
      ...
      reducers/
         index.ts
      actions/
         index.js
      types/
         actions/
           index.d.ts
      ...
      tsconfig.js
    

    文件内容-

    减速器/索引.ts

    import { Actions } from "../actions";
    

    [ts] Could not find a declaration file for module '../actions'
    

    在我的心里 类型/ 索引d.ts 我有-

    declare module "actions";
    

    {
      "compilerOptions": {
      "target": "esnext",
      "module": "commonjs",
      "jsx": "react",
    
      "strict": true,
    
      "typeRoots": ["./types", "./node_modules/@types"],
      "allowSyntheticDefaultImports": true,
      "esModuleInterop": true
     },
     "exclude": ["node_modules", "typings"]
    }
    

    所以我不知道该怎么解决-

     [ts] Could not find a declaration file for module '../actions'
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Per Svensson    7 年前

    您可以跳过模块定义技巧(.d.ts)。 尝试在重构时使用--allowJs编译器标志,typescript在理解.js代码方面做得很好。

    Ts config options

    {
        "compilerOptions": {
            "target": "esnext",
            "module": "commonjs",
            "jsx": "react",
            "strict": true,
            "allowJs": true,  //Allows you to import and compile .js files
            "typeRoots": [
                "./types",
                "./node_modules/@types"
            ],
            "allowSyntheticDefaultImports": true,
            "esModuleInterop": true
        },
        "exclude": [
            "node_modules",
            "typings"
        ]
    }
    
    推荐文章