Skip to content
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

Resulting d.ts is invalid TS code: T_1 generic type argument #40362

Closed
smolijar opened this issue Sep 3, 2020 · 0 comments · Fixed by #40483
Closed

Resulting d.ts is invalid TS code: T_1 generic type argument #40362

smolijar opened this issue Sep 3, 2020 · 0 comments · Fixed by #40483
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@smolijar
Copy link

smolijar commented Sep 3, 2020

TypeScript Version: 4.0.2 (reproduced in 4.1.0-insiders.20200903 as well)

Search Terms:

template argument, overshadow argument, template argument aliasing

Code

// Any instance type
type Client = string

// Modified instance
type UpdatedClient<C> = C & {foo: number}

export const createClient = <
  D extends
    | (new (...args: any[]) => Client) // accept class
    | Record<string, new (...args: any[]) => Client> // or map of classes
>(
  clientDef: D
): D extends new (...args: any[]) => infer C
  ? UpdatedClient<C> // return instance
  : {
      [K in keyof D]: D[K] extends new (...args: any[]) => infer C // or map of instances respectively
        ? UpdatedClient<C>
        : never
    } => {
  return null as any
}

This is the simplest MWE I could find. Strangely enough, using a generic type instead of function does not cause the error.

Expected behavior:
I expect to find this type in the resulting *.d.ts to be a valid TS code and the type of the object type branch in return to be:

{ [K in keyof D]: D[K] extends new (...args: any[]) => infer C ? UpdatedClient<C> : never; }

Actual behavior:
*.d.ts is not a valid TS code according to the same version of TS. There is this new C_1 which is a reference error.

{ [K in keyof D]: D[K] extends new (...args: any[]) => infer C ? UpdatedClient<C_1> : never; }
'C' is declared but its value is never read.
Cannot find name 'C_1'.

I think TS is probably trying to avoid C, since it assumes it's already reserved from if branch in the ternary type. Using different label does not produce the issue.

Playground Link: link

Related Issues: Maybe #40302 and #36311

smolijar added a commit to smolijar/protocat that referenced this issue Sep 3, 2020
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Sep 3, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.1.0 milestone Sep 3, 2020
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants