TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Optional parameters instantiated from generic params add `| undefined` to the type

Open ssalbdivad opened this issue 9 months ago • 9 comments

🔎 Search Terms

generic function optional undefined ? modifier return infer inferrence

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://tsplay.dev/w199kw

💻 Code

// Since 4.7, TS doesn't innately add `|undefined` to optional params the way it does for props with EOPT off

type Expected = (arg?: number) => void
//   ^? (arg?: number) => void


// But it adds it when instantiating a type from a generic param

type OptionalUnary<t> = (arg?: t) => void

type Fail = OptionalUnary<number>
//    ^? (arg?: number | undefined) => void

declare const foo: <t>(arg?: t) => void  

const instantiated = foo<number>
//    ^? (arg?: number | undefined) => void

declare const bar: <t>(arg: t) => (arg?: t) => void;

const returned = bar(42);
//    ^? (arg?: number | undefined) => void

🙁 Actual behavior

(see example)

🙂 Expected behavior

(see example)

Additional information about the issue

I don't think this is likely to cause type-safety issues. It's primarily just visual clutter and inconsistency with the standard behavior.

ssalbdivad avatar May 19 '24 22:05 ssalbdivad