Skip to content

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

Closed
@estradanic

Description

@estradanic

🔎 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions