Use missingType instead of undefinedType for optional methods under exactOptionalPropertyTypes (#52519)

This commit is contained in:
Mateusz Burzyński 2023-03-01 20:25:39 +01:00 committed by GitHub
parent dcc766f2b2
commit fc756ebc17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 148 additions and 1 deletions

View File

@ -11359,7 +11359,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return baseTypeVariable ? getIntersectionType([type, baseTypeVariable]) : type;
}
else {
return strictNullChecks && symbol.flags & SymbolFlags.Optional ? getOptionalType(type) : type;
return strictNullChecks && symbol.flags & SymbolFlags.Optional ? getOptionalType(type, /*isProperty*/ true) : type;
}
}

View File

@ -0,0 +1,31 @@
=== tests/cases/compiler/stripMembersOptionality.ts ===
// repro from #52494
declare const someVal: Required<{
>someVal : Symbol(someVal, Decl(stripMembersOptionality.ts, 2, 13))
>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --))
fn?(key: string): string | null;
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 2, 33))
>key : Symbol(key, Decl(stripMembersOptionality.ts, 3, 8))
}>;
someVal.fn("");
>someVal.fn : Symbol(fn, Decl(stripMembersOptionality.ts, 2, 33))
>someVal : Symbol(someVal, Decl(stripMembersOptionality.ts, 2, 13))
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 2, 33))
declare const someVal2: Required<{
>someVal2 : Symbol(someVal2, Decl(stripMembersOptionality.ts, 7, 13))
>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --))
fn?: (key: string) => string | null;
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 7, 34))
>key : Symbol(key, Decl(stripMembersOptionality.ts, 8, 10))
}>;
someVal2.fn("");
>someVal2.fn : Symbol(fn, Decl(stripMembersOptionality.ts, 7, 34))
>someVal2 : Symbol(someVal2, Decl(stripMembersOptionality.ts, 7, 13))
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 7, 34))

View File

@ -0,0 +1,35 @@
=== tests/cases/compiler/stripMembersOptionality.ts ===
// repro from #52494
declare const someVal: Required<{
>someVal : Required<{ fn?(key: string): string | null; }>
fn?(key: string): string | null;
>fn : ((key: string) => string | null) | undefined
>key : string
>null : null
}>;
someVal.fn("");
>someVal.fn("") : string | null
>someVal.fn : (key: string) => string | null
>someVal : Required<{ fn?(key: string): string | null; }>
>fn : (key: string) => string | null
>"" : ""
declare const someVal2: Required<{
>someVal2 : Required<{ fn?: ((key: string) => string | null) | undefined; }>
fn?: (key: string) => string | null;
>fn : ((key: string) => string | null) | undefined
>key : string
>null : null
}>;
someVal2.fn("");
>someVal2.fn("") : string | null
>someVal2.fn : (key: string) => string | null
>someVal2 : Required<{ fn?: ((key: string) => string | null) | undefined; }>
>fn : (key: string) => string | null
>"" : ""

View File

@ -0,0 +1,31 @@
=== tests/cases/compiler/stripMembersOptionality.ts ===
// repro from #52494
declare const someVal: Required<{
>someVal : Symbol(someVal, Decl(stripMembersOptionality.ts, 2, 13))
>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --))
fn?(key: string): string | null;
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 2, 33))
>key : Symbol(key, Decl(stripMembersOptionality.ts, 3, 8))
}>;
someVal.fn("");
>someVal.fn : Symbol(fn, Decl(stripMembersOptionality.ts, 2, 33))
>someVal : Symbol(someVal, Decl(stripMembersOptionality.ts, 2, 13))
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 2, 33))
declare const someVal2: Required<{
>someVal2 : Symbol(someVal2, Decl(stripMembersOptionality.ts, 7, 13))
>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --))
fn?: (key: string) => string | null;
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 7, 34))
>key : Symbol(key, Decl(stripMembersOptionality.ts, 8, 10))
}>;
someVal2.fn("");
>someVal2.fn : Symbol(fn, Decl(stripMembersOptionality.ts, 7, 34))
>someVal2 : Symbol(someVal2, Decl(stripMembersOptionality.ts, 7, 13))
>fn : Symbol(fn, Decl(stripMembersOptionality.ts, 7, 34))

View File

@ -0,0 +1,35 @@
=== tests/cases/compiler/stripMembersOptionality.ts ===
// repro from #52494
declare const someVal: Required<{
>someVal : Required<{ fn?(key: string): string | null; }>
fn?(key: string): string | null;
>fn : ((key: string) => string | null) | undefined
>key : string
>null : null
}>;
someVal.fn("");
>someVal.fn("") : string | null
>someVal.fn : (key: string) => string | null
>someVal : Required<{ fn?(key: string): string | null; }>
>fn : (key: string) => string | null
>"" : ""
declare const someVal2: Required<{
>someVal2 : Required<{ fn?: (key: string) => string | null; }>
fn?: (key: string) => string | null;
>fn : ((key: string) => string | null) | undefined
>key : string
>null : null
}>;
someVal2.fn("");
>someVal2.fn("") : string | null
>someVal2.fn : (key: string) => string | null
>someVal2 : Required<{ fn?: (key: string) => string | null; }>
>fn : (key: string) => string | null
>"" : ""

View File

@ -0,0 +1,15 @@
// @strict: true
// @exactOptionalPropertyTypes: true, false
// @noEmit: true
// repro from #52494
declare const someVal: Required<{
fn?(key: string): string | null;
}>;
someVal.fn("");
declare const someVal2: Required<{
fn?: (key: string) => string | null;
}>;
someVal2.fn("");