From adc860893e59991e07cf96758d60b22a49040e7d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 2 May 2016 16:24:06 -0700 Subject: [PATCH 1/2] Initial type of variable is union of declared type and undefined --- 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 8b53ecbf1914d..831d7960d236c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7989,7 +7989,7 @@ namespace ts { const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || getRootDeclaration(declaration).kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); + const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : addNullableKind(type, TypeFlags.Undefined)); if (strictNullChecks && !(type.flags & TypeFlags.Any) && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) { error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); // Return the declared type to reduce follow-on errors From dbfe80b3480ee6f839f797153ff8dbc49b835a9d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 2 May 2016 16:24:21 -0700 Subject: [PATCH 2/2] Fix fourslash test --- tests/cases/fourslash/quickInfoOnNarrowedType.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cases/fourslash/quickInfoOnNarrowedType.ts b/tests/cases/fourslash/quickInfoOnNarrowedType.ts index 53fe1bed232a3..2c8f4c8e216b7 100644 --- a/tests/cases/fourslash/quickInfoOnNarrowedType.ts +++ b/tests/cases/fourslash/quickInfoOnNarrowedType.ts @@ -31,8 +31,8 @@ verify.quickInfoIs('(parameter) strOrNum: string'); verify.completionListContains("strOrNum", "(parameter) strOrNum: string"); goTo.marker('4'); -verify.quickInfoIs('let s: undefined'); -verify.completionListContains("s", "let s: undefined"); +verify.quickInfoIs('let s: string | undefined'); +verify.completionListContains("s", "let s: string | undefined"); goTo.marker('5'); verify.quickInfoIs('let s: string | undefined');