Skip to content

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

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

Closed
Jym77 opened this issue Jun 20, 2022 · 4 comments
Closed

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

Jym77 opened this issue Jun 20, 2022 · 4 comments

Comments

@Jym77
Copy link

Jym77 commented Jun 20, 2022

Summary

In microsoft/TypeScript#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
Copy link
Author

Jym77 commented Jun 21, 2022

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

@zelliott
Copy link
Contributor

zelliott commented Jul 7, 2022

@iclanton it looks like you've helped with TypeScript version updates in the past (e.g. #3359, #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!

@Jym77
Copy link
Author

Jym77 commented Jul 28, 2022

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
Copy link
Author

Jym77 commented Sep 1, 2022

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 Jym77 closed this as completed Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants