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 indexed access on type with optional properties doesn't include undefined in its constraint in exactOptionalPropertyTypes #57023

Open
Andarist opened this issue Jan 11, 2024 · 2 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

Andarist commented Jan 11, 2024

πŸ”Ž Search Terms

exactOptionalPropertyTypes optional generic indexed access undefined missing contextual constraint eopt

πŸ•— Version & Regression Information

  • This changed in 5.3.0-dev.20230914

⏯ Playground Link

https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true&ts=5.4.0-dev.20240111#code/PTAEAEFMA8EMGMAuB5ADoglgewHawDYAKATlqpMYgJ4AqV5AzgFyiLECukAUFxjohQBmCSKAAaAZUSwBAYVyCMAc3bEZ2HKADeXUKwAWkALaQA-CwBEsdoiwXQAH1AWAJrGIBre04v5l+xAsAbl1QADcMSAB3eXx2IxxzZwAjSAYMF0hvZwRMMKyQgF8eTPh8d1FBdhwkDVAlSEQANQZ4LEz5HEUVNUxcAB4aAD4ACgZIWtwWBjY+JQBKFhpHUGrMxRxIFxCuGFQsSlAqmr7NBsRO7tV1AYA5WBNQGAEcFwZQD0gqLEFxKRlIJdlNdTqNQngTCx7iYADRcRZ-aRyBTA3oaADa0MgAF1tKE2jgZvVIJtiBh4ABBBjpJR4ZIYPzUGhpRAsSRIwEono3HCYh440AAXlWr0gGy2QVAICexFIxB4emIjVUZ0aLTaHS5II0IwhkHmkulFDlXGKXAJRIJ8CVAipNLpDIwTJZbP+yK6qJ56M+31+7IBQO5p1xwrWYr4EqlYCwHi4QA

πŸ’» Code

// @exactOptionalPropertyTypes: true

interface XStateConfiguration {
  theme?: "auto" | "dark" | "light";
  viewColumn?: "beside" | "active";
}

declare function getVscodeConfiguration<T>(section: string): T | undefined;

export function getConfiguration<Name extends keyof XStateConfiguration>(
  name: Name,
): XStateConfiguration[Name] {
  const genericAssignabilityTest: XStateConfiguration[Name] = undefined; // error

  return getVscodeConfiguration(name); // error
}

const concreteAssignabilityTest: XStateConfiguration[keyof XStateConfiguration] = undefined; // ok

πŸ™ Actual behavior

undefined is not assignable to an indexed access like this in expression space

πŸ™‚ Expected behavior

I'd expect this to typecheck OK - just like the concrete example

Additional information about the issue

No response

@fatcerberus
Copy link

Hmm, that's tricky because doesn't XStateConfiguration[Name] necessarily have different read and write types (as long as it remains generic) under EOPT?

@Andarist
Copy link
Contributor Author

Yes, it does. I didn't spend time thinking about it yet but likely a PR of mine broke this: #55585

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases and removed Bug A bug in TypeScript Help Wanted You can do this labels Jan 11, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

3 participants