typescript第三方库没有导出类型,我们该如何使用呢?

248 阅读1分钟

假设有个第三方库的源码:



type MyType = { a: string, b: number }

export const fn = (props: MyType) :void => {
       ...
}

我们想在自己的代码中使用这个MyType,但它又没有export出来,该怎么办呢?

只需善用Utility Types

type Props = Parameters<typeof fn>[0]

如果想拿到MyType里的a:

type Props = NonNullable<Parameters<typeof fn>[0]>['a']