使用新接口扩展(更新)第三方旧类型声明接口

Extend (Update) third-party old type declaration interface with new one

我的问题是我使用的是旧类型声明包 (@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'

我在 google 和打字稿论坛上搜索过它,但没有任何有意义的答案。是否可以更新具有相同道具的界面?还是我需要等到公司在 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"
  ]
}

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

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

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