microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.16k stars 12.38k forks source link

Never returning functions should disable getter return requirement #52214

Open ASDFGerte opened 1 year ago

ASDFGerte commented 1 year ago

Suggestion

declare const error: () => never;
const o = { get p() { error(); } };

Produces the error A 'get' accessor must return a value.(2378). Throwing an error directly would not. I feel similar to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#better-support-for-never-returning-functions, a never-returning-function should have the same effect.

Afaik "control flow analysis happens before typechecking", so this would only work, when the function has a declared type. That's in line with the linked existing improvement in TS 3.7, and to be expected.

🔍 Search Terms

Getter Get accessor must return a value never returning function error throw

✅ Viability Checklist

My suggestion meets these guidelines:

fabiospampinato commented 1 year ago

Also this error shouldn't happen if one has noImplicitReturns turned off and the return type of the getter set to void or undefined.