From c054cbb64d55cb8cd456b6def155e86fa9da5016 Mon Sep 17 00:00:00 2001 From: Thomas den Hollander Date: Thu, 10 Jan 2019 19:39:43 +0100 Subject: [PATCH] Fix strict-type-predicate for unknown (#4444) * Add tests for strict-type-predicates and unknown * Fix unknown with strict-type-predicate * Fix unknown literal --- src/rules/strictTypePredicatesRule.ts | 7 +++- .../strict-null-checks/test.ts.lint | 3 ++ .../unknown/test.ts.lint | 42 +++++++++++++++++++ .../unknown/tsconfig.json | 5 +++ .../unknown/tslint.json | 5 +++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/rules/strict-type-predicates/unknown/test.ts.lint create mode 100644 test/rules/strict-type-predicates/unknown/tsconfig.json create mode 100644 test/rules/strict-type-predicates/unknown/tslint.json diff --git a/src/rules/strictTypePredicatesRule.ts b/src/rules/strictTypePredicatesRule.ts index bd07d69bd58..2a878137f2a 100644 --- a/src/rules/strictTypePredicatesRule.ts +++ b/src/rules/strictTypePredicatesRule.ts @@ -99,7 +99,12 @@ function walk(ctx: Lint.WalkContext, checker: ts.TypeChecker): void { const exprType = checker.getTypeAtLocation(exprPred.expression); // TODO: could use checker.getBaseConstraintOfType to help with type parameters, but it's not publicly exposed. - if (isTypeFlagSet(exprType, ts.TypeFlags.Any | ts.TypeFlags.TypeParameter)) { + if ( + isTypeFlagSet( + exprType, + ts.TypeFlags.Any | ts.TypeFlags.TypeParameter | ts.TypeFlags.Unknown, + ) + ) { return; } diff --git a/test/rules/strict-type-predicates/strict-null-checks/test.ts.lint b/test/rules/strict-type-predicates/strict-null-checks/test.ts.lint index 57c624c8a4d..2f45d333a2f 100644 --- a/test/rules/strict-type-predicates/strict-null-checks/test.ts.lint +++ b/test/rules/strict-type-predicates/strict-null-checks/test.ts.lint @@ -194,6 +194,9 @@ declare function get(): T; typeof get() === `stirng`; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [typeof] + typeof get() === "unknown"; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [typeof] + let a: string, b: string; typeof a === typeof b; typeof a === b; diff --git a/test/rules/strict-type-predicates/unknown/test.ts.lint b/test/rules/strict-type-predicates/unknown/test.ts.lint new file mode 100644 index 00000000000..40dd5d0da99 --- /dev/null +++ b/test/rules/strict-type-predicates/unknown/test.ts.lint @@ -0,0 +1,42 @@ +[typescript]: >= 3.0.0 + +declare function get(): T; + +// typeof +{ + typeof get() === "undefined"; + typeof get() === "boolean"; + typeof get() === "number"; + typeof get() === "string"; + typeof get() === "symbol"; + typeof get() === "function"; + typeof get() === "object"; +} + +// negation +{ + get() !== null; + get() !== undefined; +} + +// reverse left/right +{ + "string" === typeof get(); + + undefined === get(); +} + +// type parameters +{ + function f(t: T) { + typeof t === "boolean"; + } +} + +const body: unknown = 'test'; +if (typeof body === 'object') + console.log('a'); + +let test: unknown = undefined; +if (test !== undefined) + console.log('b'); diff --git a/test/rules/strict-type-predicates/unknown/tsconfig.json b/test/rules/strict-type-predicates/unknown/tsconfig.json new file mode 100644 index 00000000000..1cc3bc85ee9 --- /dev/null +++ b/test/rules/strict-type-predicates/unknown/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "strictNullChecks": true + } +} \ No newline at end of file diff --git a/test/rules/strict-type-predicates/unknown/tslint.json b/test/rules/strict-type-predicates/unknown/tslint.json new file mode 100644 index 00000000000..bb5f9231e91 --- /dev/null +++ b/test/rules/strict-type-predicates/unknown/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "strict-type-predicates": true + } +}