How To Add File With Extending Prototype In Typescript
Suppose i want to extend String.prototype, so i have this in ext/string.ts for example: interface String { contains(sub: string): boolean; } String.prototype.contains = functi
Solution 1:
You just need to run the file without importing anything. You can do that with this code:
import"./ext/string";
However, if your string.ts
file contains any import statements then you will need to take out the interface and put it in a definition file (.d.ts
). You need to do this with external modules so that the compiler knows it needs to be merged with the String
interface in the global scope. For example:
// customTypings/string.d.ts
interface String {
contains(sub: string): boolean;
}
// ext/string.ts
String.prototype.contains = function(sub:string): boolean{
if (sub === "") {
return false;
}
return (this.indexOf(sub) !== -1);
};
// main.ts
import "./ext/string";
"some string".contains("t"); // true
Post a Comment for "How To Add File With Extending Prototype In Typescript"