我有以下代码:
import { ExampleType } from "@org/package-1
type AnotherType = ExampleType & number
我希望ExampleType
也包含在tsc
命令行工具生成的类型定义输出中。
输出应该像这样:
// dist/types.d.ts
type ExampleType = string | boolean
type AnotherType = ExampleType & number
我正在使用typescript v4.6.4
,这就是我的tsconfig。Json看起来像:
{
"include": [
"./src/types",
"./src/exceptions",
"./src/config",
"./src/index.ts",
"./src/Client.tsx",
"./src/Whatever.ts",
"./node_modules/@org/package1",
"./node_modules/@org/package2"
],
"exclude": [],
"compilerOptions": {
"types": ["react", "react/jsx-runtime"],
"jsx": "react-jsx",
"baseUrl": "./src",
"target": "ES6",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"module": "ESNext",
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"noImplicitAny": false,
"lib": [
"ES2017",
"DOM",
"DOM.Iterable"
],
"allowJs": true,
"declarationDir": "./dist/types",
"resolveJsonModule": true,
"emitDeclarationOnly": true,
"declaration": true,
"skipLibCheck": true,
"stripInternal": true,
"noResolve": true
},
}
模块应该导出它想要公开的类型。这些类型来自哪里并不重要。它可以由模块内部构造,也可以从其他模块导入。
这没什么特别的。只需导入模块需要的类型,并导出模块要公开的类型。
例如:
import { ExampleType } from "@org/package-1" // use in this file
export { ExampleType } from "@org/package-1" // export a type from another module
export type AnotherType = ExampleType & number // use an imported type in an exported type
export type YetAnotherType = { abc: number } // export a totally internal type