Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

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

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'

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?

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"
  ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...