Microsoft/TypeScript
GitHub ã§èŠã[Regression] Failed to assign to a discriminated union class property when its type is inferred from constructor
Open
#60,394 opened on 2024幎11æ2æ¥
BugDomain: check: Type InferenceHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
property infer constructor discriminated
ð Version & Regression Information
- This changed between versions 4.3.5 and 4.4.4 (also in latest)
⯠Playground Link
ð» Code
type State = { type: 'running', speed: number } | { type: 'stopped' };
class Actor {
private state;
// ^? Actor.state: State
constructor() {
this.state = { type: 'stopped' } as State;
}
run(speed: number) {
this.state = { type: 'running', speed }
}
}
ð Actual behavior
Type 'string' is not assignable to type '"running" | "stopped"'.(2322)
ð Expected behavior
No error, as in 4.3.5.
Additional information about the issue
If the property's type is explicitly declared as private state: State the error is gone.