【问题标题】:Extend (Update) third-party old type declaration interface with new one用新的扩展(更新)第三方旧类型声明接口
【发布时间】:2019-03-01 08:14:00
【问题描述】:

我的问题是我使用的是旧的类型声明包 (@types/expo)。所以这就是为什么我需要更新它的某些部分。我像这样创建了一个新的打字文件。 (./typings/expo/index.d.ts)

import * as expo from 'expo';

declare module 'expo' {
  var Icon: any;
  var SplashScreen: any;

  export interface AppLoadingProps {
    startAsync?: () => Promise<void[]>;
  }
}

一些部件开始工作,但我也开始收到此错误:

[ts] Subsequent property declarations must have the same type. 
Property 'startAsync' must be of type '(() => Promise<void>) | undefined', 
but here has type '(() => Promise<void[]>) | undefined'

我在谷歌和打字稿论坛上搜索过它,但它没有任何有意义的答案。是否可以更新具有相同道具的界面?还是我需要等到公司在definitelyTyped 上更新它的包?

我的 tsconfig.json 文件;

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "es2015",
    "lib": [ /* Specify library files to be included in the compilation. */
      "es2017",
      "dom"
    ],
    "jsx": "react-native",
    "importHelpers": true,
    "strict": true,
    "noImplicitAny": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "moduleResolution": "node",
    "typeRoots": [ /* List of folders to include type definitions from. */
      "./typings",
      "./node_modules/@types"
    ],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "noEmitHelpers": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "build/dist"
  },
  "exclude": [
    "build",
    "node_modules"
  ],
  "types": [
    "typePatches"
  ]
}

【问题讨论】:

    标签: typescript typescript-typings expo


    【解决方案1】:

    如果您需要覆盖现有的属性声明来更改类型,则需要 fork @types/expo 类型。

    最简单的方法可能是将index.d.ts 文件复制到您的typings 目录并卸载原始@types/expo 包。或者你可以使用Braid之类的工具(披露:我是一个Braid贡献者)直接从DefinitelyTyped存储库中导入types/expo/index.d.ts文件;这样做的好处是可以很容易地将上游更新与您自己的修改合并,但如果 DefinitiveTyped 无论如何都会很快更新,这对您来说可能无关紧要。

    无论哪种方式,您都可以选择调整 baseUrl and paths options 以便模块解析找到您的 index.d.ts 文件,或者为修改后的 @types/expo 包创建 package.json 并将其注册为主 @ 中的依赖项987654333@ 使用相对路径。

    【讨论】:

    • 谢谢!不幸的是,正如您所说,没有简单的方法可以避免这种情况。我认为这是打字稿中最难的部分之一。
    猜你喜欢
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2021-11-18
    • 2020-01-12
    相关资源
    最近更新 更多