rushstack icon indicating copy to clipboard operation
rushstack copied to clipboard

[api-extractor] parsing error on `(infer T extends U)` pattern

Open Jym77 opened this issue 3 years ago • 3 comments

Summary

In https://github.com/microsoft/TypeScript/issues/49517 I reported a TS bug and was given a workaround which, unfortunately seems to break api-extractor 😢

My .d.ts file:

declare class Foo {
    protected foo: number;
}
declare class Bar {
    protected foo: number;
}
declare type Nothing<V extends Foo> = void;
/**
 * @internal
 */
export declare type Broken<V extends Array<Foo | Bar>> = {
    readonly [P in keyof V]: V[P] extends (infer T extends Foo) ? Nothing<T> : never;
};
export {};
//# sourceMappingURL=index.d.ts.map

Running api-extractor, I get the error:

ERROR: Internal Error: Unable to follow symbol for ""
You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.

I assume it is on the (infer T extends Foo) pattern that it breaks, given that it used to work fine on older version of the code that was just using V[P] instead. Additionally, it seems that prettier also breaks because it expects a ? after the inner extends, so my guess it that api-extractor has a similar problem 🤔

Repro steps

Creating a fresh TS project, install api-extractor with default config (api-extractor init). Use the following TS file:

class Foo {
  protected foo = 0;
}

class Bar {
  protected foo = 0;
}

type Nothing<V extends Foo> = void;

/**
 * @internal
 */
export declare type Broken<V extends Array<Foo | Bar>> = {
  // breaks TS, see https://github.com/microsoft/TypeScript/issues/49517
  // readonly [P in keyof V]: V[P] extends Foo ? Nothing<V[P]> : never;
  
  // workaround, breaks API extractor
  readonly [P in keyof V]: V[P] extends (infer T extends Foo) ? Nothing<T> : never;
};

Run api-extractor --local

Expected result: API is successfully extracted

Actual result:

ERROR: Internal Error: Unable to follow symbol for ""
You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.

Details

Diagnostic: diagnostic.txt

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@microsoft/api-extractor version? 7.25.2
Operating system? Windows
API Extractor scenario?
Would you consider contributing a PR? No
TypeScript compiler version? 4.7.4
Node.js version (node -v)? 16.14.2

Jym77 avatar Jun 20 '22 14:06 Jym77

Apparently, this seems to be valid syntax only in TS 4.7 and API extractor is still using 4.6 😕

Jym77 avatar Jun 21 '22 07:06 Jym77

@iclanton it looks like you've helped with TypeScript version updates in the past (e.g. https://github.com/microsoft/rushstack/pull/3359, https://github.com/microsoft/rushstack/pull/3078)? I started a local branch to try to bump the TypeScript version, copying what appeared to have been done in past attempts, but am hitting a slew of eslint errors that look like this:

[eslint] Error: libraries/node-core-library/src/InternalError.ts - Parsing error: node.getStart is not a function [eslint] Error: libraries/node-core-library/src/TypeUuid.ts - Parsing error: node.getStart is not a function [eslint] Error: libraries/node-core-library/src/AlreadyReportedError.ts - Parsing error: Cannot read properties of undefined (reading 'declarations') ...

I imagine I'm doing something wrong. Here's my WIP branch: https://github.com/microsoft/rushstack/compare/main...zelliott:rushstack:upgrade-typescript?expand=1... some help would be greatly appreciated!

zelliott avatar Jul 07 '22 14:07 zelliott

Still on TS 4.7 syntax, I'm also running in troubles when trying to use variance annotation.

export declare class Calculation<out D = string> {
    static of(): Calculation;
}

gives me

 (TS2707) Generic type 'Calculation<out, D>' requires between 1 and 2 type arguments.

so it looks like the variance annotation is parsed as an extra type argument.

Jym77 avatar Jul 28 '22 10:07 Jym77

TS has been updated to 4.7 in API extractor 7.29, and I'm not seeing these problems anymore 🎉

I'm closing the issue ✔️

Jym77 avatar Sep 01 '22 12:09 Jym77