Microsoft/TypeScript

Generic indexed access on type with optional properties doesn't include `undefined` in its constraint in `exactOptionalPropertyTypes`

Open

#57.023 aberto em 11 de jan. de 2024

Ver no GitHub
 (2 comments) (0 reactions) (0 assignees)TypeScript (6.726 forks)batch import
Domain: flag: exactOptionalPropertyTypesHelp WantedPossible Improvement

Métricas do repositório

Stars
 (48.455 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)

Description

🔎 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

Guia do colaborador