我正在处理一个没有类型的包,所以我创建了一个类型定义。这个包是react-date-range,所以我创建了一个目录来放置类型定义:src/types/react-date-range/index.d.ts
,一切都很好。
但是,因为我需要更新react-date-range
的源代码,我分叉了这个项目,并用修改创建了一个新的npm包。这个包是一个有作用域的公共包,现在它的名字是@example-org/react-date-range
。
我正在尝试更新类型定义以匹配新名称,但我得到错误。
我将模块名称改为declare module "@example-org/react-date-range"
而不是declare module "react-date-range"
,但它不起作用。我也试着把类型定义在src/types/@example-org/react-date-range/index.d.ts
和src/types/@example-org__react-date-range/index.d.ts
上,但这都不起作用。
这是我的配置文件
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"baseUrl": "src",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
我分叉了这个项目,创建了一个新的npm包,修改为
你可以提交一个pull request到DefinitelyTyped项目,这样就可以安装你创建的类型:
npm install --save-dev @types/react-date-range
DefinitelyTyped项目中的包的作用域是@types
, TypeScript会自动在node_modules/@types
下找到类型定义。
如果您想从包中加载类型定义,那么您需要在compilerOptions
typeRoots
中指定它。{
"compilerOptions": {
"typeRoots": ["./node_modules/@types/", "./node_modules/@example-org/react-date-range"]
}
}
查看typescript网站上的typeroot文档