Angular 自定义类型扩展方法typescript
我已经为此挣扎了一段时间,我试图在angular 8中向自定义类型添加扩展方法,但是我无法编译它们。有人能告诉我我做错了什么吗Angular 自定义类型扩展方法typescript,angular,typescript,extension-methods,Angular,Typescript,Extension Methods,我已经为此挣扎了一段时间,我试图在angular 8中向自定义类型添加扩展方法,但是我无法编译它们。有人能告诉我我做错了什么吗 export interface MyCustomType{ name:string; id:number; } 我所尝试的: 一, 此文件未编译,但出现以下错误: 找不到源文件中的错误: 未使用与上面相同的错误消息编译 有人能告诉我如何向我创建的接口添加扩展方法吗?没有一个像矩或数组一样另一种方法是导出类而不是接口: export class My
export interface MyCustomType{
name:string;
id:number;
}
我所尝试的:
一,
此文件未编译,但出现以下错误:
找不到源文件中的错误:
未使用与上面相同的错误消息编译
有人能告诉我如何向我创建的接口添加扩展方法吗?没有一个像矩或数组一样另一种方法是导出类而不是接口:
export class MyCustomType {
name: string;
id: number;
toString(): string {
return 'this should work';
}
}
接口在运行时不存在(在传输到javascript时被擦除)。所以你不能扩展它(除非有一个类/值)
declare module 'mytype'{
export interface MyCustomType{
toString():string;
}
}
MyCustomType.prototype.toString() = funtion(){
return 'this should work';
}
export class MyCustomType {
name: string;
id: number;
toString(): string {
return 'this should work';
}
}