microsoft / microsoft/TypeScript

Error from `exactOptionalPropertyTypes` not reported when using object spread with a ternary and optional property acces

Ouverte
#63,240 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: flag: exactOptionalPropertyTypes
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔎 Search Terms

exactOptionalPropertyTypes
object spread
ternary

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about exactOptionalPropertyTypes, object spread.
⏯ Playground Link

https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true&ts=5.9.3&ssl=5&ssc=32&pln=1&pc=1#code/C4TwDgpgBAYg9nKBeKBvAUFKYCGAnCAO2AEkATAfgC4oBnYPAS0IHN0BfddMiAYwBt80XnEL0oBAI4BXCPQBCcMiBrw4Abi78IwKMHwsdqhJvQB6M3QAWcAO60oEAB6RewCGUd48cPFDKyeogAok44bgDyYMCMojj8AAo+kHigACrgcuj6eIa6KKjYQsTkNFKyCkogAHS4BCWe7KYWUAC2jLS0zCxePn62VkRQ0l2sdGAEOJ44hJ7ueIT4INkGOshoUNVbABQMgRQbdUSkZGUQMnLAisq1xSdQ7FA0qOwAlA-qUC3yAKoA4lwRGJdIUjg0HutypdriBNDk8utCltqrs8PtDndSkV6vdHs83h8sF9LL8AeZLBBvL4oDgwJB8A4RN4+MB+CAoIwAGZQABu8UCHSghDguhw2GSlNANN4vDktA8UE5PlaNMIUDgACMAFYs9BA8Rgk4AJnWAHJTTSHPQmGMAD7DWYQTnMDxw1b5DbI1Ho0GY07Y47kE14tAEproIA

💻 Code
type Foo = {
  parentId?: string
}

declare const requestBody: Foo;

let target: Foo;

// shows expected error due to ExactOptionalPropertyTypes
target = { parentId: requestBody.parentId };

// missing error when using spread and ternary
target = { ...(true ? { parentId: requestBody.parentId } : {}) }; // BUG

const { parentId } = requestBody;
target = { ...(true ? { parentId: parentId } : {}) };   // BUG

// error appears correctly if value is not a property accessed from an object
const parentId2 = '' as string | undefined;
target = { ...(true ? { parentId: parentId2 } : {}) };

🙁 Actual behavior

The expected error is silenced only in the specific case that combines object spread, ternary, and accessing an optional property from an object, although the types are always recognized correctly with | undefined.

🙂 Expected behavior

All scenarios demonstrated in the code snippet are expected to show the same error.

Additional information about the issue

No response

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.

Piste de recherche

Commencez par le TypeScript Playground lié et exécutez la reproduction de exactOptionalPropertyTypes. Comparez les cas d’affectation directe, de ternaire avec spread d’objet, de propriété déstructurée et de variable locale ; l’issue est terminée lorsque les cas de spread et de ternaire signalent la même erreur attendue que les autres scénarios.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

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