You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionpanic(): never{thrownewError('Oh no!');}exportconsterrors={panic(): never{thrownewError('Oh no!');},// also tried these// panic: (): never => { throw new Error('Oh no!'); },// panic,}// also tried these//} as const;//} satisfies Record<string, (() => never)>;functionmaybeGetString(): string|null{return"I'm a string!";}functiontest(){consta=maybeGetString();if(!a)errors.panic();// if (!a) panic(); // the error goes away if you use this insteadconstb: string=a;console.log(b);}
π Actual behavior
A function returning never is being treated differently when it is called from inside an object i.e. panic vs errors.panic. This is despite the fact that they both resolve to the same type () => never.
π Expected behavior
The function should work the same way regardless of how it is called. It should work the same as a throw statement, so that in the code snippet above, the type of a is narrowed from string | never to string, and there should be no error on the statement const b: string = a
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
never, throw, error, unreachable
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
A function returning never is being treated differently when it is called from inside an object i.e.
panic
vserrors.panic
. This is despite the fact that they both resolve to the same type() => never
.π Expected behavior
The function should work the same way regardless of how it is called. It should work the same as a
throw
statement, so that in the code snippet above, the type ofa
is narrowed fromstring | never
tostring
, and there should be no error on the statementconst b: string = a
The text was updated successfully, but these errors were encountered: