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

Properly discriminate by pattern literal types in isTypeInvalidDueToUnionDiscriminant #61350

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14453,7 +14453,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const nameType = property.name && (isJsxNamespacedName(property.name) ? getStringLiteralType(getTextOfJsxAttributeName(property.name)) : getLiteralTypeFromPropertyName(property.name));
const name = nameType && isTypeUsableAsPropertyName(nameType) ? getPropertyNameFromType(nameType) : undefined;
const expected = name === undefined ? undefined : getTypeOfPropertyOfType(contextualType, name);
return !!expected && isLiteralType(expected) && !isTypeAssignableTo(getTypeOfNode(property), expected);
return !!expected && (isLiteralType(expected) || isPatternLiteralType(expected)) && !isTypeAssignableTo(getTypeOfNode(property), expected);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the logic in createUnionOrIntersectionProperty here. This HasLiteralType is required for Discriminant:

Discriminant = HasNonUniformType | HasLiteralType,

});
}

Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/completionsDiscriminatedUnion2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

// @strict: true

//// type A = {
//// id: `A:${string}`;
//// a: number;
//// };
////
//// type B = {
//// id: `B:${string}`;
//// b: number;
//// };
////
//// function foo<T extends A | B>(item: T): void {}
////
//// foo({ id: "B:", /**/ });

verify.completions({ marker: "", exact: ["b"] });