+1 vote
ago by
Angular application.
I'm trying to add a method to the Number type:

tsconfig.json:
...
"typeRoots": [
"node_modules/@types",
"types"
]
...
types/number.d.ts:
export {}

declare global {
interface Number {
isInt() : boolean
}
}
number.extension.ts:
Number.prototype.isInt = function() {
return (Math.round(this) == this)
}
And, when I try to compile, I get errors:
Error: number.extensions.ts:16:18 - error TS2339: Property 'isInt' does not exist on type 'Number'.
[ng] 16 Number.prototype.isInt = function() {
[ng] ~~~~~
[ng] Error: node_modules/typescript/lib/lib.es5.d.ts:395:5 - error TS2300: Duplicate identifier 'trimMiddle'.
[ng] 395 trimMiddle: () => any;
[ng] ~~~~~~~~~~
[ng] Error: node_modules/typescript/lib/lib.es5.d.ts:396:5 - error TS2411: Property 'anchor' of type '(name: string) => string' is not assignable to string index type '() => any'.
[ng] 396 [x: string]: () => any;
[ng] ~~~~~~~~~~~~~~~~~~~~~~~
[ng] Error: node_modules/typescript/lib/lib.es5.d.ts:396:5 - error TS2411: Property 'charAt' of type '(pos: number) => string' is not assignable to string index type '() => any'.
[ng] 396 [x: string]: () => any;
[ng] ~~~~~~~~~~~~~~~~~~~~~~~
[ng] Error: node_modules/typescript/lib/lib.es5.d.ts:396:5 - error TS2411: Property 'charCodeAt' of type '(index: number) => number' is not assignable to string index type '() => any'.
[ng] 396 [x: string]: () => any;

2 Answers

0 votes
ago by
 
Best answer
As it turns out, everything with my code is fine.
For some unknown reason, the compiler hashed it. I had to uninstall and reinstall.
0 votes
ago by
There is already a built-in isInteger method, why write your own, and even in the prototype Number?
https://developer.mozilla.org/ru/docs/Web/JavaScri...
ago by
eewynell , The question is not whether to check integer or not, the question is how to extenuate the base type Number
ago by
This method is given only as an example
...