'Declaring a global array extension to check an empty list leads to a compiler error
I learning Typescript and I'm trying to create convenient features with the Array extension. This example below works on the Typescript playground but my editor throws TypeError: ... is not a function. All other extensions work well but read an empty list I can't in this way. I tried many solutions from StackOverflow but it does not work anyway. Someone can explain how it works?
declare global {
interface Array<T> {
isNotEmpty(): boolean;
isEmpty(): boolean;
}
}
Array.prototype.isNotEmpty = function <T>(this: T[]): boolean {
return !!this.length;
};
Array.prototype.isEmpty = function <T>(this: T[]): boolean {
return !this.length;
};
export { };
Solution 1:[1]
Create a new declarations file, e.g. Array.d.ts with the declaration you're adding:
declare global {
interface Array<T> {
isNotEmpty(): boolean;
isEmpty(): boolean;
}
}
Then under the include property in your tsconfig.json add the path to that .d.ts file like so:
"include": [
"src/*.ts", // Or wherever your source code lies
"Array.d.ts" // <- this is the new declaration
],
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
