I'm trying to create a TypeScript definition file for this String.format for JavaScript implementation, but I'm struggling.
I'm still new to TypeScript and I've read many articles/snippets on how to create definitions. One of the best resources I've found is Steve Fenton's blog article Complex TypeScript Definitions Made Easy. I've followed the suggested examples but have so far been unsuccessful.
The problem is because the external Javascript adds a method to "String". I've tried to "declare" the String class as follows:
declare class String {
declare function format(str: string, obj0: any, obj1?: any, obj2?: any): string;
}
This results in the error: Duplicate identifier 'String'
I've also tried to describe the type information using an interface:
interface String {
format(str: string, obj0: any, obj1?: any, obj2?: any): string;
}
The interface itself seems ok but if I try to call:
String.format("The {0} brown {1}!", "quick", "fox")
I get the error The property 'format' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'.
I'm assuming this is because format is a static method (in TypeScript terms anyway), but is defined as an instance method on the interface (since interfaces can't contain static method definitions).
I may be going in totally the wrong direction with this, or may have a totally flawed understanding of some element of Javascript, TypeScript or both. Can anybody at least nudge me in the right direction please?