TypeScript
TypeScript copied to clipboard
Auto accessor field types cannot be inferred from constructor
🔎 Search Terms
auto accessor, type inference
🕗 Version & Regression Information
- This is the behavior in every version I tried since auto accessors were introduced (4.9)
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/MYGwhgzhAEBiD29oG8BQ0PTMYBTK8ATtAG5ggCuuq6mw8AdhAC6EXDNEAUDAXNAwoBbAEa5CAShS1MmZgAsAlhAB0ZSrmgBeATVmzFAM2g9oAHmgAGKWn13oC5WvJVtAiiBAz9AX29+-IA
💻 Code
class Foo {
value // Inferred: number | null
constructor(n: number) {
this.value = n
if (n < 0) {
this.value = null
}
}
}
class Foo {
accessor value // Error: Member 'value' implicitly has an 'any' type.
constructor(n: number) {
this.value = n
if (n < 0) {
this.value = null
}
}
}
🙁 Actual behavior
TS reports that auto accessor field has type 'any'
🙂 Expected behavior
TS should infer field type from constructor
Additional information about the issue
No response