第三方旧类型声明接口

第三方旧类型声明接口

本文介绍了用新的扩展(更新)第三方旧类型声明接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is that I'm using an old type declaration package (@types/expo). So that's why I need to update some part of it. I created a new typing file like this. (./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[]>;
  }
}

有些零件开始工作,但我也开始出现此错误:

Some parts were started to work but also I started to get this error:

[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上更新其程序包?

I searched it on google and typescript forums but It doesn't have any meaningful answer for this. Is it possible to update the interface that has same props? Or do I need to wait until the company updates its package on definitelyTyped?

我的tsconfig.json文件;

my tsconfig.json file;

{
  "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类型.

If you need to overwrite an existing property declaration to change the type, you'll need to fork the @types/expo types.

最简单的方法可能是将index.d.ts文件复制到您的typings目录中,然后卸载原始的@types/expo软件包.或者,您可以使用辫子之类的工具(披露:我是辫子贡献者)来导入直接从DefinitelyTyped存储库中的types/expo/index.d.ts文件;这样做的好处是可以很容易地将上游更新与您自己的修改合并在一起,但是如果DefinitelyTyped仍将很快更新,这对您可能并不重要.

The easiest way is probably to copy the index.d.ts file into your typings directory and uninstall the original @types/expo package. Or you can use a tool such as Braid (disclosure: I am a Braid contributor) to import the types/expo/index.d.ts file directly from the DefinitelyTyped repository; this has the advantage that it's easy to merge upstream updates with your own modifications, but this may not matter to you if DefinitelyTyped is going to be updated soon anyway.

无论哪种方式,您都可以选择调整 baseUrlpaths选项,以便模块解析找到您的index.d.ts文件或为修改后的@types/expo包创建一个package.json,并使用相对路径.

Either way, you have the option to adjust your baseUrl and paths options so that module resolution finds your index.d.ts file or to create a package.json for your modified @types/expo package and register it as a dependency in your main package.json using a relative path.

这篇关于用新的扩展(更新)第三方旧类型声明接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:17