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

Produce a more complete union of string literal template interpolations when the same const appears multiple times #49132

Open
tlebon opened this issue May 16, 2022 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@tlebon
Copy link

tlebon commented May 16, 2022

Bug Report

When using a constant union of two types, using the value below will always infer it to still be either or both types although as a const it can only be one.

🔎 Search Terms

duplicate key union

🕗 Version & Regression Information

Seen in most recent version of TS (4.6.2) and every version available on the playground.

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Unions
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

const key = 'guest' as 'guest'| 'service';

const obj = {
    guestguest: 'hello',
    serviceservice: 'goobdye'
}

obj[`${key}${key}`]

🙁 Actual behavior

Typescript throws error

Element implicitly has an 'any' type because expression of type '"guestguest" | "guestservice" | "serviceguest" | "serviceservice"' can't be used to index type '{ guestguest: string; serviceservice: string; }'. Property 'guestservice' does not exist on type '{ guestguest: string; serviceservice: string; }'.(7053)

as this is a const, it is impossible for it to still be an either/or, so multiple instances of the variable should all be equal.

In this case, only guestguest or serviceservice should be possible

🙂 Expected behavior

Typescript should correctly infer the key options to be only a combination of one of the options, and not any possible combination.

@tlebon tlebon changed the title Typescript misses type inference from a destructured Union Typescript misses type inference from a Union May 16, 2022
@RyanCavanaugh
Copy link
Member

How did this happen in real code?

@tlebon
Copy link
Author

tlebon commented May 16, 2022

trying to get the names of an i18n json that was called something like guestpanelguestoption or servicepanelserviceoption depending on what feature is used. the name is typed based on the keyof typeof the file.

const string = t(`${key}panel${key}option`)

the option only exists for a single case, not a mixed option. ie. always guest-guest or service-service
the original key (from the example) was a string that only expected guest or service as the response.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 16, 2022
@RyanCavanaugh RyanCavanaugh changed the title Typescript misses type inference from a Union Produce a more complete union of string literal template interpolations when the same const appears multiple times May 16, 2022
@bschnelle
Copy link

bschnelle commented Apr 11, 2023

Any updates on this? I believe we're running into a similar problem as shown in the example below.

const obj = {
  a: '1',
  b: '2',
} as const;

type Obj = typeof obj;
type ObjKeys = keyof Obj;
type ObjPaths = { [K in ObjKeys]: `${K} | ${Obj[K]}`}[ObjKeys];

const getObjPath = <K extends ObjKeys, V extends Obj[K]>(k: K, v: V) => {
  return `${k} | ${v}` satisfies ObjPaths; // unable to correctly infer
}

getObjPath('a', '2'); // correctly infers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants