在尝试在其他地方发布的建议后,我发现自己无法运行使用无类型NPM模块的打字稿项目.下面是一个最小的例子和我尝试的步骤.
对于这个最小的例子,我们假装lodash没有现有的类型定义.因此,我们将忽略该包@types/lodash并尝试手动将其打包文件添加lodash.d.ts到我们的项目中.
文件夹结构
接下来,文件.
文件 foo.ts
///<reference path="../typings/custom/lodash.d.ts" />
import * as lodash from 'lodash';
console.log('Weeee');
Run Code Online (Sandbox Code Playgroud)
文件lodash.d.ts直接从原始@types/lodash包复制.
文件 index.d.ts
/// <reference path="custom/lodash.d.ts" />
/// <reference path="globals/lodash/index.d.ts" />
Run Code Online (Sandbox Code Playgroud)
文件 package.json
{
"name": "ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"typings": "./typings/index.d.ts",
"dependencies": {
"lodash": "^4.16.4"
},
"author": "",
"license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)
文件 tsconfig.json
{
"compilerOptions": {
"target": …Run Code Online (Sandbox Code Playgroud) 我有一个名为的文件service.ts,它公开以下代码:
export interface SomeInterface {
keyOne: string;
}
export class TestService<T = SomeInterface> {
property: T;
}
Run Code Online (Sandbox Code Playgroud)
在index.ts文件中,我正在使用该服务:
import { TestService } from './service';
const service = new TestService();
service.property.keyOne
Run Code Online (Sandbox Code Playgroud)
我还创建index.d.ts了SomeInterface用更多键声明相同接口的文件:
export interface SomeInterface {
keyTwo: number;
}
Run Code Online (Sandbox Code Playgroud)
问题是service.property只有“知道”keyOne属性。我怎么能告诉打字稿合并他们两个?