Skip to content

Commit 36513f2

Browse files
committed
Remove only undefined, not null | undefined, from declared type
1 parent 533ce82 commit 36513f2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10929,7 +10929,7 @@ namespace ts {
1092910929
declaration.initializer &&
1093010930
getFalsyFlags(declaredType) & TypeFlags.Undefined &&
1093110931
!(getFalsyFlags(checkExpression(declaration.initializer)) & TypeFlags.Undefined);
10932-
return annotationIncludesUndefined ? getNonNullableType(declaredType) : declaredType;
10932+
return annotationIncludesUndefined ? getTypeWithFacts(declaredType, TypeFacts.NEUndefined) : declaredType;
1093310933
}
1093410934

1093510935
function checkIdentifier(node: Identifier): Type {

tests/cases/compiler/defaultParameterAddsUndefinedWithStrictNullChecks.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ function foo4(x: string | undefined = undefined, b: number) {
2727
x = undefined;
2828
}
2929

30+
type OptionalNullableString = string | null | undefined;
31+
function allowsNull(val: OptionalNullableString = "") {
32+
val = null;
33+
val = 'string and null are both ok';
34+
}
35+
allowsNull(null); // still allows passing null
36+
3037

3138

3239
// .d.ts should have `string | undefined` for foo1, foo2, foo3 and foo4

0 commit comments

Comments
 (0)