-
Notifications
You must be signed in to change notification settings - Fork 653
Description
Motivation
From the perspective of an API consumer, it's useful to know whether an API item is "readonly". Consider the following example:
export class Example {
a = 'hello';
readonly b = 'hello'
}
const example = new Example();
example.a = 'goodbye'; // Allowed
example.b = 'goodbye'; // Not allowed
It's useful for an API consumer to know that example.b = 'goodbye'; results in a TypeScript compilation error.
However, determining whether something is readonly is not as simple as just looking for the TypeScript readonly keyword. Consider the following examples:
export class Example {
readonly a = 'hello';
get b() { return 'hello'; }
get c() { return 'hello'; }
/** @readonly */
set c(value: string) { throw new Error('c is readonly'); }
}
export const d = 'hello';
The examples a - d above are all examples of "readonly" properties/variables for which it would make sense to include a readonly property in their respective doc model nodes. From the perspective of an API consumer, these items are all readonly in some way (and in particular, examples a, b, and c are essentially functionally equivalent from their perspective).
Brief design
We'll define an api-extractor-model mixin similar to ApiStaticMixin called ApiReadonlyMixin. This mixin will be applied to ApiVariable, ApiProperty, etc. Then, we'll update api-extractor to assign this property accordingly. Note that there are some open questions around when this assignment should be done.
References
For the original Zulip discussion behind this FR, please see https://rushstack.zulipchat.com/#narrow/stream/262521-api-extractor/topic/Checking.20if.20an.20item.20is.20readonly. Credit to @octogonz for coming up with the examples above as well as the brief design strategy.