get and set Accessors Are Allowed in Ambient Contexts
In previous versions of TypeScript, the language didn’t allow get
and set
accessors in ambient contexts (like in declare
-d classes, or in .d.ts
files in general).The rationale was that accessors weren’t distinct from properties as far as writing and reading to these properties;however, because ECMAScript’s class fields proposal may have differing behavior from in existing versions of TypeScript, we realized we needed a way to communicate this different behavior to provide appropriate errors in subclasses.
As a result, users can write getters and setters in ambient contexts in TypeScript 3.6.
declare class Foo {
// Allowed in 3.6+.
get x(): number;
set x(val: number): void;
}
In TypeScript 3.7, the compiler itself will take advantage of this feature so that generated .d.ts
files will also emit get
/set
accessors.