Hacker News new | past | comments | ask | show | jobs | submit login

I really think the second one shouldn't raise a type error.

This is already invalid:

  function foo1(arg1?: number, arg2: number) {}
In that case you have to use

  function foo1(arg1: number | undefined, arg2: number) {}

  foo1(undefined,1);

But other than that what is the use case?



The second one raises an error:

https://www.typescriptlang.org/play?#code/AQMwrgdgxgLglgewqB...

This conflation of "optional" and "undefined-able" is more obvious in interfaces. This is why TypeScript added the `exactOptionalPropertyTypes` option:

https://www.typescriptlang.org/tsconfig#exactOptionalPropert...

Sometimes "is not defined" needs to be treated differently than "is set to undefined"




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: