Closed as not planned
Description
π Search Terms
constructor, initialization, initialized, 2564, never, throw, error
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
const throwParseError = (def: unknown): never => {
throw new Error(`${def} is not a valid definition`)
}
class Foo {
// Property 'bar' has no initializer and is not definitely assigned in the constructor.(2564)
bar: string
constructor(def: string | null) {
if (def === null) {
// this return pattern is useful for narrowing
// if you remove `return` here, the initialization error goes away
// but the assignment to bar is then an error
return throwParseError(null)
}
this.bar = def
}
}
π Actual behavior
TypeScript reports that Property 'bar' has no initializer and is not definitely assigned in the constructor.(2564)
π Expected behavior
TypeScript should identify that in every viable branch of the constructor, bar
is assigned.
Additional information about the issue
No response