Skip to content

Optional parameters instantiated from generic params add | undefined to displayed typeΒ #58578

Open
@ssalbdivad

Description

@ssalbdivad

πŸ”Ž 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions