Typescript: Unexpected TS2365: Operator '!==' cannot be applied to types '"string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"String"'.

Created on 19 Apr 2017  路  1Comment  路  Source: microsoft/TypeScript



TypeScript Version: 2.2.2

Code
Maybe the same as https://github.com/Microsoft/TypeScript/issues/12794

export function convertAllFloat2Int<T>(json: any): T {
        if (typeof json !== "String") {
            json = JSON.stringify(json);
        }

        json = JSON.parse(json, function (key: any, value: any) {
            let result = value;
            if ($.isNumeric(value)) {
                result = ~~value;
            }

            return result;
        });

        return json;
    }
// A *self-contained* demonstration of the problem follows...

Expected behavior:
No compile error as this is a valid JS code or a hint how to solve this issue
Actual behavior:
The following error message on this line 'if (typeof json !== "String") {' :
Operator '!==' cannot be applied to types '"string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"String"'.

Working as Intended

Most helpful comment

Did you mean typeof json !== "string"? Notice that typeof operator returns string without a capital letter.

The error basically tells you that this comparison is always true, because the return value of typeof is never the string you compare it to.

>All comments

Did you mean typeof json !== "string"? Notice that typeof operator returns string without a capital letter.

The error basically tells you that this comparison is always true, because the return value of typeof is never the string you compare it to.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  路  3Comments

manekinekko picture manekinekko  路  3Comments

bgrieder picture bgrieder  路  3Comments

siddjain picture siddjain  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments