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

Generic properties are not correctly inferred #57318

Closed
markostanimirovic opened this issue Feb 7, 2024 · 2 comments
Closed

Generic properties are not correctly inferred #57318

markostanimirovic opened this issue Feb 7, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@markostanimirovic
Copy link

markostanimirovic commented Feb 7, 2024

πŸ”Ž Search Terms

"generic type inference"

πŸ•— Version & Regression Information

TS v5.3.3

It's not a regression.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAysBOBLAdgcwArwPZgM4B4BRZYRUAPigF4oBvAKCigG0BpKFKAawhCwDMoxUqCgBDXEJJkQbALpQIAD2ARkAE0m4EKVFAD8UdgC4oyCADcI8OaeEz5AbnoBfZ-VCRYOtJhyteImkKam5eAW8kX2w8IJEQcnd+AFdkAGNSLGQoABssVACQInIACh4QUzgojBjC4oBKOkYoNKzcLByIADo81DLeeucXenoU9Mzs62x4IkUVNU06KGTkxHVTbWqoF1LGhiZeusJSgHIVtZPB13ogA

πŸ’» Code

type StringProps<Entity> = {
  [K in keyof Entity as Entity[K] extends string ? K : never]: Entity[K];
};

type StringPropKey<Entity> = keyof StringProps<Entity>;

function logKey<E>(key: StringPropKey<E>) {
  console.log(key);
}

function error<E extends { uuid: string }>() {
  logKey<E>('uuid');
}

πŸ™ Actual behavior

Compilation error: Argument of type 'string' is not assignable to parameter of type 'keyof StringProps<E>'. Type '"uuid"' is not assignable to type '(E[K] extends string ? K : never) | (E["uuid"] extends string ? "uuid" : never)'

πŸ™‚ Expected behavior

There should not be a compilation error. As a workaround, we can explicitly check if E extends { uuid: string }:

function error<E extends { uuid: string }>() {
  // this
  logKey<E extends { uuid: string } ? E : never>('uuid');
}

but this should not be required because E already extends { uuid: string }.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Feb 7, 2024

Seems like you want #48992; TS doesn't have the kind of abstract reasoning necessary to understand what StringProps<E> does for generic/arbitrary E.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 7, 2024
@markostanimirovic
Copy link
Author

closing as duplicate

@markostanimirovic markostanimirovic closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants