TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

exactOptionalPropertyTypes adds `undefined` to `Required` types in JavaScript but not in TypeScript.

Open jespertheend opened this issue 3 years ago • 1 comments

Bug Report

🔎 Search Terms

required undefined javascript

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about required

⏯ Playground Link

💻 Code

/**
 * @typedef Foo
 * @property {number} [foo]
 */

const x = /** @type {Foo} */ ({});
x.foo; // number | undefined

const y = /** @type {Required<Foo>} */ ({});
y.foo; // number | undefined

🙁 Actual behavior

There's a mismatch between the JavaScript and TypeScript behaviour which I think is unintentional. y.foo has type number | undefined in JavaScript but in TypeScript its type is number.

🙂 Expected behavior

I'm guessing JavaScript has the incorrect behaviour here. So in that case the expected result would be just number.

jespertheend avatar Sep 21 '22 15:09 jespertheend

I think this issue is related to https://github.com/microsoft/TypeScript/issues/49080. I've added an additional test to https://github.com/microsoft/TypeScript/pull/49910

/cc @RyanCavanaugh @sandersn

a-tarasyuk avatar Sep 21 '22 17:09 a-tarasyuk

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar Sep 24 '22 18:09 typescript-bot