Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with mapped property symbol not displaying added | undefined when its origin symbol was optional #59957

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6104,9 +6104,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
case SyntaxKind.JSDocPropertyTag:
if (!isOptionalDeclaration(declaration)) {
return false;
}
symbol ??= getSymbolOfDeclaration(declaration);
if (!(symbol.flags & SymbolFlags.Property) || !(symbol as MappedSymbol).links?.mappedType) {
return false;
}
const type = getTypeOfSymbol(symbol);
return !!(symbol.flags & SymbolFlags.Property && symbol.flags & SymbolFlags.Optional && isOptionalDeclaration(declaration) && (symbol as MappedSymbol).links?.mappedType && containsNonMissingUndefinedType(type));
if (!containsNonMissingUndefinedType(type)) {
return false;
}
const declaredType = getEffectiveTypeAnnotationNode(declaration);
return !!(declaredType && !containsUndefinedType(getTypeFromTypeNodeWithoutContext(declaredType)));
case SyntaxKind.Parameter:
case SyntaxKind.JSDocParameterTag:
return requiresAddingImplicitUndefined(declaration, enclosingDeclaration);
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/fourslash/quickInfoMappedPropertyUnionUndefined1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path="./fourslash.ts"/>

// @strict: true
// @exactOptionalPropertyTypes: true

// https://github.com/microsoft/TypeScript/issues/59948

//// type OptionalToUnionWithUndefined<T> = {
//// [K in keyof T]: T extends Record<K, T[K]> ? T[K] : T[K] | undefined;
//// };
////
//// type Intermidiate/*1*/ = OptionalToUnionWithUndefined<{ a?: string }>;
//// type Literal/*2*/ = { a?: string | undefined };
////
//// type Res1/*3*/ = Required<Intermidiate>;
//// type Res2/*4*/ = Required<Literal>;

verify.quickInfoAt("1", `type Intermidiate = {
a?: string | undefined;
}`);
verify.quickInfoAt("2", `type Literal = {
a?: string | undefined;
}`);
verify.quickInfoAt("3", `type Res1 = {
a: string | undefined;
}`);
verify.quickInfoAt("4", `type Res2 = {
a: string | undefined;
}`);
26 changes: 26 additions & 0 deletions tests/cases/fourslash/quickInfoMappedPropertyUnionUndefined2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path="./fourslash.ts"/>

// @strict: true

//// type OptionalToUnionWithUndefined<T> = {
//// [K in keyof T]: T extends Record<K, T[K]> ? T[K] : T[K] | undefined;
//// };
////
//// type Intermidiate/*1*/ = OptionalToUnionWithUndefined<{ a?: string }>;
//// type Literal/*2*/ = { a?: string | undefined };
////
//// type Res1/*3*/ = Required<Intermidiate>;
//// type Res2/*4*/ = Required<Literal>;

verify.quickInfoAt("1", `type Intermidiate = {
a?: string | undefined;
}`);
verify.quickInfoAt("2", `type Literal = {
a?: string | undefined;
}`);
verify.quickInfoAt("3", `type Res1 = {
a: string;
}`);
verify.quickInfoAt("4", `type Res2 = {
a: string;
}`);
13 changes: 13 additions & 0 deletions tests/cases/fourslash/quickInfoMappedPropertyUnionUndefined3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="./fourslash.ts"/>

// https://github.com/microsoft/TypeScript/issues/60411

// @strict: true

//// type UnsetUndefinedToOblivion<T> = { [P in keyof T]-?: T[P] | undefined };
//// type SetUndefined<T> = { [P in keyof T]: T[P] | undefined };
//// type TheWhat/**/ = SetUndefined<UnsetUndefinedToOblivion<{ a?: 1 }>>;

verify.quickInfoAt("", `type TheWhat = {
a: 1 | undefined;
}`);