-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inline *_base declaration in d.ts files #59550
Comments
I hit this again. It makes it really annoying for users of Fluid Framework's SharedTree to use API-Extractor since doing so requires really annoying workarounds to export schema. A schema definition like /**
* Do not use. Exists only as a workaround for {@link https://github.com/microsoft/TypeScript/issues/59550} and {@link https://github.com/microsoft/rushstack/issues/4429}.
* @system @alpha
*/
export const _APIExtractorWorkaroundJsonArrayBase = sf.arrayRecursive("array", JsonUnion);
/**
* Docs here....
* @alpha
*/
export class JsonArray extends _APIExtractorWorkaroundJsonArrayBase {} Having to do this for every shared tree schema makes a real mess schema files and the package API surface. |
Also, in general it is difficult to detect these example.ts class A_base {}
class A extends class {} {} example.d.ts declare class A_base {
}
declare const A_base_1: {
new (): {};
};
declare class A extends A_base_1 {
} The fundamental challenge is that, by design, API Extractor processes .d.ts files and does not consider their .ts inputs. I agree that, this case, emitting an algebraic type might make the .d.ts file actually easier to interpret. |
I hit this again. I'd love to see my suggested fix implemented: I'm not aware of any downsides to it. I think the code that needs to change is TypeScript/src/compiler/transformers/declarations.ts Lines 1659 to 1703 in 739d729
I suspect that entire if/else could be removed and use my suggested format in both cases, or just replace the TypeScript/src/compiler/transformers/declarations.ts Lines 1660 to 1691 in 739d729
|
I have created #61087 with an implementation of this change. |
π Search Terms
Anonymous class, _base, base, d.ts, api-extractor, recursive
β Viability Checklist
β Suggestion
Currently classes which extends anonomous classes compile to d.ts files with two declarations:
Generates:
playground link
My suggestion is for it to generate something like this instead:
More generally replace:
with:
π Motivating Example
When exporting classes with anonymous bases (for example classes with bases generated using functions, for example in fluid-framework's tree schema system, it would be nice if the generated d.ts file better matched the original source, so tools (like TypeScript and API-Extractor) behave more similarly to how they would if run on the source instead of on the d.ts. This would make the d.ts better serve as a concise summary of the type information of the original code.
π» Use Cases
What do you want to use this for?
I know of two cases this would help:
_base
class inserted by compiler is not exported.Β rushstack#4429 by removing this odd case from d.ts files.What shortcomings exist with current approaches?
Currently the behavior is confusing since most TS developers don't think about the differences between the d.ts files and the original source, so having to do workarounds to make them stay aligned (like manually exporting the base to make API-Extractor happy, or ensuring that if the base is not inline, that the code still type checked for the recursive case) is confusing and unintuitive.
What workarounds are you using in the meantime?
For API extractor, manually export the base type under a different name.
For the recursive type issue, export carefully crafted usage before the declaration which happens to cause it to compile with the split declaration.
The text was updated successfully, but these errors were encountered: