Skip to content

Unexpected broadening of generic type parameter when used in the types of 2 different function params #57427

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

Closed
estradanic opened this issue Feb 17, 2024 · 2 comments

Comments

@estradanic
Copy link

🔎 Search Terms

type broadening, type narrowing, generics, generic type parameter, index type, template literal, typescript, unexpected, why doesn't typescript know that these are the same, same generic instance not enforced between function params

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about the same generic instance type being used in different function parameters

⏯ Playground Link

https://www.typescriptlang.org/play#code/C4TwDgpgBAyg9gWwsAFgSwHYHMoF4oDk8SqmWBA3AFCiSyLLrYCiANgM7T5EOkscRKVGuGgAVCAjCsAhsAhjReKAANijMgDoAJAG91fLAF8VUAD6qDTLG0479va7YgnhAMwCuGAMbA0cDCgAEzgrMgB1NFQAWRAxawAeMSgIAA95DCD2ehJrcxyNfk4APgAKNzQIViCALigxABooODA-ALqxAEooXSoofqhvAPZgKAQ4yWk5CA7J2XlFOnwVPQqqoKN7FraME2ojKiA

💻 Code

type Something = 'Something';
type SomethingElse = 'SomethingElse';

type TemplateType = `Something.${Something}` | `SomethingElse.${SomethingElse}`

function doSomethingWithMyThing<T extends Something | SomethingElse>(field: T, option: T) {
    const myTemplate: TemplateType = `${field}.${option}`;
}

🙁 Actual behavior

The actual type of ${field}.${option} is some sort of cartesian product of the types of field and option, causing the TemplateType type of myTemplate to be incorrect.

🙂 Expected behavior

The type of ${field}.${option} should be either "Something.Something" or "SomethingElse.SomethingElse" based on the same T being enforced between the two params.

Additional information about the issue

No response

@fatcerberus
Copy link

fatcerberus commented Feb 17, 2024

See #57372 (comment).
T can legally be instantiated to Something | SomethingElse in which case the parameters don't have to correlate. You're looking for #27808.

@estradanic
Copy link
Author

Interesting. That is frustrating, but it makes sense. Thanks for the link!

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