[英]Custom npm package in typescript: cannot find name of custom interface
我有使用 webpack 構建的打字稿中的 angular 應用程序。 我也有下一個結構的 npm 包(托管在 Sinopia 中,而不是 npm)
my-custom-npm-package
- index.ts
- studentStorage.ts
- student.d.ts
學生.d.ts
interface IStudent
{
name: string;
id: number;
}
索引.ts
import { StudentStorage } from './studentStorage';
export { StudentStorage }
studentStorage.ts
export class StudentStorage{
private students: IStudent[] = [];
public get(): IStudent[]{
return this.students.slice(0);
}
}
在我的角度組件中,我有這個字符串
import { StudentStorage } from 'my-custom-npm-package';
接下來我用 webpack 收到這個錯誤
ERROR in D:\Sandbox\MultiLanguage\node_modules\my-custom-npm-package\studentStorage.ts
(2,23): error TS2304: Cannot find name 'IStudent'.
我有解決方法:在我寫的地方創建typings.d.ts
/// <reference path="node_modules/my-custom-npm-package/student.d.ts" />
但是我想在沒有此修復程序的情況下使用我的自定義 npm 包。 我想在不引用 .d.ts 文件的情況下進行安裝。
我怎樣才能做到這一點?
一種解決方案可能是將/// <reference path="student.d.ts" />
到studentStorage.ts
。 但是讓IStudent
全局化而你的庫的其余部分在模塊中是很奇怪的。 我建議將student.d.ts
更改為一個模塊:
export interface IStudent
{
name: string;
id: number;
}
和studentStorage.ts
到:
import { IStudent } from "./student";
export class StudentStorage{
private students: IStudent[] = [];
public get(): IStudent[]{
return this.students.slice(0);
}
}
然后,想要使用IStudent
圖書館客戶只需導入它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.