Remove only undefined, not null | undefined, from declared type

This commit is contained in:
Nathan Shively-Sanders
2017-03-07 09:14:51 -08:00
parent 533ce824e8
commit 36513f21ab
2 changed files with 8 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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