TS2709

error TS2709: Cannot use namespace ‘globalThis‘ as a type.

Broken Code ❌

1
2
3
4
5
function parseNumber(this: globalThis, input: string): number {
return this.parseInt(input);
}

parseNumber.call(this, '100');

Fixed Code ✔️

You cannot use a namespace as a type, but you can get the type assigned with that namespace by using the typeof operator:

1
2
3
4
5
function parseNumber(this: typeof globalThis, input: string): number {
return this.parseInt(input);
}

parseNumber.call(this, '100');