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

Union with a mapped string template results in an unexpected type #50120

Closed
cboar opened this issue Aug 1, 2022 · 2 comments
Closed

Union with a mapped string template results in an unexpected type #50120

cboar opened this issue Aug 1, 2022 · 2 comments

Comments

@cboar
Copy link

cboar commented Aug 1, 2022

Bug Report

🔎 Search Terms

union template string literal unexpected behavior

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about unions leading to unexpected behavior.

⏯ Playground Link

Playground link with relevant code

💻 Code

type A = { [P in `/${string}`]: string };
type B = { [P in 'one' | 'two' | 'three']: number };
type C = A | B;

// should not allow partial! But it is accepted, why?
const bug1: C = { one: 1 };
// should not be able to mix A and B! But it is accepted, why?
const bug2: C = { one: 1, '/hello': 'world' };

🙁 Actual behavior

Observe the mapped type A that uses a string template as an index, allowing only specific strings to be used as a key. When this type is used as part of a union, the resulting type exhibits behavior I found strange.

🙂 Expected behavior

I would expect the type assignment for bug1 and bug2 to not be permitted.

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 1, 2022

Duplicate of #20863. Both your object literals are compatible with the type A, so assigning is permitted.

@cboar
Copy link
Author

cboar commented Aug 1, 2022

Thanks, I wasn't aware of this fact: Unions without discriminant properties do not perform excess property checking.

@cboar cboar closed this as completed Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants