From a80d70fc11f62f2ddfc575347ebff451a03a3c60 Mon Sep 17 00:00:00 2001 From: Thomas den Hollander Date: Mon, 7 Jan 2019 19:32:35 +0100 Subject: [PATCH 1/3] Add tests for strict-type-predicates and unknown --- .../unknown/test.ts.lint | 34 +++++++++++++++++++ .../unknown/tsconfig.json | 5 +++ .../unknown/tslint.json | 5 +++ 3 files changed, 44 insertions(+) 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/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..8ff2d8eda00 --- /dev/null +++ b/test/rules/strict-type-predicates/unknown/test.ts.lint @@ -0,0 +1,34 @@ +[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"; + } +} 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 + } +} From 7d14552028129f3f381a8ebd3cd62614e51feb39 Mon Sep 17 00:00:00 2001 From: Thomas den Hollander Date: Mon, 7 Jan 2019 19:47:39 +0100 Subject: [PATCH 2/3] Fix unknown with strict-type-predicate --- src/rules/strictTypePredicatesRule.ts | 10 +++++++++- test/rules/strict-type-predicates/unknown/test.ts.lint | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/rules/strictTypePredicatesRule.ts b/src/rules/strictTypePredicatesRule.ts index bd07d69bd58..443b5fa6264 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; } @@ -239,6 +244,8 @@ function getTypePredicateForKind(kind: string): Predicate | undefined { return flagPredicate(ts.TypeFlags.ESSymbol); case "function": return isFunction; + case "unknown": + return flagPredicate(ts.TypeFlags.Unknown); case "object": // It's an object if it's not any of the above. const allFlags = @@ -247,6 +254,7 @@ function getTypePredicateForKind(kind: string): Predicate | undefined { ts.TypeFlags.BooleanLike | ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike | + ts.TypeFlags.Unknown | ts.TypeFlags.ESSymbol; return type => !isTypeFlagSet(type, allFlags) && !isFunction(type); default: diff --git a/test/rules/strict-type-predicates/unknown/test.ts.lint b/test/rules/strict-type-predicates/unknown/test.ts.lint index 8ff2d8eda00..40dd5d0da99 100644 --- a/test/rules/strict-type-predicates/unknown/test.ts.lint +++ b/test/rules/strict-type-predicates/unknown/test.ts.lint @@ -32,3 +32,11 @@ declare function get(): 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'); From e6305eb6e26dd38bcd77ce84b734df9dd5253dd0 Mon Sep 17 00:00:00 2001 From: Thomas den Hollander Date: Mon, 7 Jan 2019 19:55:31 +0100 Subject: [PATCH 3/3] Fix unknown literal --- src/rules/strictTypePredicatesRule.ts | 3 --- .../strict-type-predicates/strict-null-checks/test.ts.lint | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rules/strictTypePredicatesRule.ts b/src/rules/strictTypePredicatesRule.ts index 443b5fa6264..2a878137f2a 100644 --- a/src/rules/strictTypePredicatesRule.ts +++ b/src/rules/strictTypePredicatesRule.ts @@ -244,8 +244,6 @@ function getTypePredicateForKind(kind: string): Predicate | undefined { return flagPredicate(ts.TypeFlags.ESSymbol); case "function": return isFunction; - case "unknown": - return flagPredicate(ts.TypeFlags.Unknown); case "object": // It's an object if it's not any of the above. const allFlags = @@ -254,7 +252,6 @@ function getTypePredicateForKind(kind: string): Predicate | undefined { ts.TypeFlags.BooleanLike | ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike | - ts.TypeFlags.Unknown | ts.TypeFlags.ESSymbol; return type => !isTypeFlagSet(type, allFlags) && !isFunction(type); default: 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;