microsoft / microsoft/TypeScript

Removing optional modifier in homomorphic mapped types does not work in generic contexts since 5.5.x

Ouverte
#59,902 0 commentaires 0 réactions 1 personne assignée Voir sur GitHub

@ahejlsberg y travaille déjà.

Depuis le 9/9/2024.

Bug Domain: Mapped Types
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔎 Search Terms

optional modifier, required fields, generic, NonNullable, strictNullChecks, homomorphic mapped types

🕗 Version & Regression Information
  • This changed between versions 5.4.5 and 5.5.2
  • This changed in commit or PR e418f8d12c5f1b6c10fc3127764f34dad44d4586 (as reported by every-ts)
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.4#code/MYewdgzgLgBApgDwIYFsAOAbOMC8MA8AKvAlHGACYQzQBOAlmAOYB8AFAJQBcMSYAnrhYwA3gCgYMAPRSYAJTiYkwRkxgB3elAAWvATTRwVAM3rAaAVwBGAWij9DMEMZgADOqtcwUSANZxqHWx7Q2pQWlojWCQmJEYAOglpWRDsYjwPZgBuMSTUmABlEBQ4AHk0KHpwJAxcURgAbX9BRhhCAF0Afh4ARhgAXxzJPIdsAEEMDAUARwt6SIo6kUbmmFbm50LisoqqsBr2m26YPsHcyXyZuYWANRqLALqJqbhZ+bgKJrh+Teer94o7RySRkMAAwsUlIxqK0AKzxAAs8QQABoYFYLLBTGBsHD4vCEElQJBYAgeP9bvdHngLJQ4NiPucYJEoBZaGAYAgcoMgA

💻 Code
const example = <T extends string>(): any => {
  // Replacing with any specific sub-type of `string` makes the types correct again.
  // type T = string;

  type SomeOptional = { [key in T]?: 1 };
  
  type AllRequired = { [key in keyof SomeOptional]-?: 1 };

  type RequiredValues = AllRequired[keyof AllRequired];

  // Complains in 5.4.x, but fine in 5.5.x
  const x: RequiredValues = undefined

  return x;
};
🙁 Actual behavior

Using -? leaves the fields marked as optional, and taking the values of the resulting type gives a union with undefined.

🙂 Expected behavior

Using -? makes all fields required, and taking the values of the resulting type gives a union of field types.

Additional information about the issue

Requires a --strictNullChecks flag.

Replacing keyof SomeOptional with NonNullable<keyof SomeOptional> or keyof SomeOptional as keyof SomeOptional fixes the issue. It looks like this problem is limited to homomorphic mapped types.

Using Required<{ [P in keyof SomeOptional]: 1; }> in place of { [key in keyof SomeOptional]-?: 1 } results in the same behaviour.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.