diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f17f079008b..8ba023ce05a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10929,7 +10929,7 @@ namespace ts { declaration.initializer && getFalsyFlags(declaredType) & TypeFlags.Undefined && !(getFalsyFlags(checkExpression(declaration.initializer)) & TypeFlags.Undefined); - return annotationIncludesUndefined ? getNonNullableType(declaredType) : declaredType; + return annotationIncludesUndefined ? getTypeWithFacts(declaredType, TypeFacts.NEUndefined) : declaredType; } function checkIdentifier(node: Identifier): Type { diff --git a/tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts b/tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts index 04a9e668b32..d2e167c83a3 100644 --- a/tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts +++ b/tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts @@ -27,6 +27,13 @@ function foo4(x: string | undefined = undefined, b: number) { x = undefined; } +type OptionalNullableString = string | null | undefined; +function allowsNull(val: OptionalNullableString = "") { + val = null; + val = 'string and null are both ok'; +} +allowsNull(null); // still allows passing null + // .d.ts should have `string | undefined` for foo1, foo2, foo3 and foo4