From 498568b16f5b8eef0f70a96d4816d98001f68ed9 Mon Sep 17 00:00:00 2001 From: Slawomir Sadziak Date: Wed, 28 Dec 2016 05:18:53 +0100 Subject: [PATCH 1/2] #13063 Fix strictNullChecks breaking typeof * Allow typeof to use not-auto variable in strictNullChecks mode --- src/compiler/checker.ts | 2 +- tests/baselines/reference/typeofStrictNull.js | 8 ++++++++ tests/baselines/reference/typeofStrictNull.symbols | 9 +++++++++ tests/baselines/reference/typeofStrictNull.types | 9 +++++++++ tests/cases/compiler/typeofStrictNull.ts | 4 ++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/typeofStrictNull.js create mode 100644 tests/baselines/reference/typeofStrictNull.symbols create mode 100644 tests/baselines/reference/typeofStrictNull.types create mode 100644 tests/cases/compiler/typeofStrictNull.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 139f3f052cb..c08a44b347e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10372,7 +10372,7 @@ namespace ts { // the entire control flow graph from the variable's declaration (i.e. when the flow container and // declaration container are the same). const assumeInitialized = isParameter || isOuterVariable || - type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0) || + type !== autoType && type !== autoArrayType && (!strictNullChecks || isInTypeQuery(node) || (type.flags & TypeFlags.Any) !== 0) || isInAmbientContext(declaration); const flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); // A variable is considered uninitialized when it is possible to analyze the entire control flow graph diff --git a/tests/baselines/reference/typeofStrictNull.js b/tests/baselines/reference/typeofStrictNull.js new file mode 100644 index 00000000000..520a72b9f0f --- /dev/null +++ b/tests/baselines/reference/typeofStrictNull.js @@ -0,0 +1,8 @@ +//// [typeofStrictNull.ts] + +let a: number; +let b: typeof a; + +//// [typeofStrictNull.js] +var a; +var b; diff --git a/tests/baselines/reference/typeofStrictNull.symbols b/tests/baselines/reference/typeofStrictNull.symbols new file mode 100644 index 00000000000..7b2dfb41797 --- /dev/null +++ b/tests/baselines/reference/typeofStrictNull.symbols @@ -0,0 +1,9 @@ +=== tests/cases/compiler/typeofStrictNull.ts === + +let a: number; +>a : Symbol(a, Decl(typeofStrictNull.ts, 1, 3)) + +let b: typeof a; +>b : Symbol(b, Decl(typeofStrictNull.ts, 2, 3)) +>a : Symbol(a, Decl(typeofStrictNull.ts, 1, 3)) + diff --git a/tests/baselines/reference/typeofStrictNull.types b/tests/baselines/reference/typeofStrictNull.types new file mode 100644 index 00000000000..59344727987 --- /dev/null +++ b/tests/baselines/reference/typeofStrictNull.types @@ -0,0 +1,9 @@ +=== tests/cases/compiler/typeofStrictNull.ts === + +let a: number; +>a : number + +let b: typeof a; +>b : number +>a : number + diff --git a/tests/cases/compiler/typeofStrictNull.ts b/tests/cases/compiler/typeofStrictNull.ts new file mode 100644 index 00000000000..ede2b857305 --- /dev/null +++ b/tests/cases/compiler/typeofStrictNull.ts @@ -0,0 +1,4 @@ +// @strictNullChecks: true + +let a: number; +let b: typeof a; \ No newline at end of file From 5317f13c165e84881f25a8a44b282bf103914e42 Mon Sep 17 00:00:00 2001 From: Slawomir Sadziak Date: Wed, 28 Dec 2016 20:22:24 +0100 Subject: [PATCH 2/2] #13063 Optimization Add isInTypeQuery as the last OR --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c08a44b347e..a944918f9a3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10372,7 +10372,7 @@ namespace ts { // the entire control flow graph from the variable's declaration (i.e. when the flow container and // declaration container are the same). const assumeInitialized = isParameter || isOuterVariable || - type !== autoType && type !== autoArrayType && (!strictNullChecks || isInTypeQuery(node) || (type.flags & TypeFlags.Any) !== 0) || + type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node)) || isInAmbientContext(declaration); const flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); // A variable is considered uninitialized when it is possible to analyze the entire control flow graph