-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inline *_base declaration in d.ts files #59550
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
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. |
The TypeScript team suggested I make a PR to implement my suggestion if I wanted progress to be made on this issue. That is the PR linked above. They then reviewed my proposal and communicated to me indirectly that my proposal exploits a parser bug since expressions are not permitted in ambient contexts (my entire suggestion was to cram an expression in an ambient context, so this makes sense). This makes it difficult to find a solution since existing versions of TypeScript give an error "Expression expected.(1109)" if you provide something other than an expression in that location. The requirement for existing TypeScript versions that you provide an expression for the extends clause (in To me this seems like future versions of TypeScript would have to make a breaking change to the parser/grammar to redefine what is allowed in To make my change to be compatible with this future breaking change to TypeScript grammar, I'd like to know what would be accepted in this context by these theoretical future TypeScript versions? My understanding is that in ambient contexts, we allow Thus am I correct in assuming that if future versions of TypeScript want to make a breaking change to what's allowed in extends clauses of Assuming the above is correct, it seems like the design of the There is an interesting workaround for that problem however, which is also backwards compatible with existing versions of TypeScript: you allow syntaxes for declare class:
This makes it possible to give a types directly to the extends clause without using an expression (in compilers with the new grammer), while keeping most existing code still working (provides expressions compatible with typeof). This new syntax also just happens to work with existing compilers (they parse it as an expression), so generating it wouldn't cause compatibility issues. Changing the parser to stop supporting expressions in this context is still a breaking change, but the above approach could allow my workaround to continue to function with or without that breaking change ensuring forwards and backwards compatibility for my suggestion here, even if other expressions are banned. |
π 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: