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

instantiation expression usage leading to an invalid d.ts file generation #60758

Open
AlexisTessier opened this issue Dec 13, 2024 · 4 comments Β· May be fixed by #60796
Open

instantiation expression usage leading to an invalid d.ts file generation #60758

AlexisTessier opened this issue Dec 13, 2024 · 4 comments Β· May be fixed by #60796
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@AlexisTessier
Copy link

AlexisTessier commented Dec 13, 2024

πŸ”Ž Search Terms

partial function generic application, function generic variant from another function, instantiation-expressions

πŸ•— Version & Regression Information

  • This changed between versions 5.6.2 and 5.7.2

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.2#code/C4TwDgpgBAyg9gWwgBQE50q0BpCIDOUAvFAEoQCGAJnAHYA2IAgqqhSADxoYRYi4gAfAChQkKE2AcmUCAA9gEWlULlqdRizacArrQDWtOAHdaggDRQAkrIVKVUWjoQAjXsUfO3qQR6YBtKwBdYWF5MDgsKAAzPQBjYABLOigECkTaAQBxHQpUKg5hKChsW0VlQlRKGgYQKH9uTBw8SwA6dvgkRt5mgiDzYUEACiKofTx8AC4SgYBKaY4AFTL7QjgXACsIBOHRgDcKeh0IacW54l8Do+hEwgBRBTYEpctyOMiCyQ5sSwAGCygekMJjMvgA3qMqsAdKhaFAlisKlB1lsdiNisUrsdTgNivMoFibvdHhRnmcyNsPtIpD8oP9LECjKZBL4iODRsUoTC4eMCP5fkEoBkCYdjqMAL7CSWicDQABicDgHjBMUVAEZpvhgKgMgBzSzRRUAJk12r1UElYmgACE8sqoC48hqoFqdbR9Q68iaXWb3RaZeIFXBbagPEGoAAfKAh4TvWhaz2oASEEj+ADkjtQarTlgzXrTgoohDjWrCcgiURLwCF+BDHjSGWyuXyQ0zydmHCDIZEoSAA

πŸ’» Code

type SomePropertyKeys = ReadonlyArray<PropertyKey>
type At<A extends ReadonlyArray<unknown>, I extends number = number> = A[I]

export function mainKeyGuard<
  K extends readonly [PropertyKey, ...SomePropertyKeys],
>(
  keys: K,
): <T extends object>(
  value: T,
) => value is Extract<T, Record<At<K, 0>, unknown>> {
  return <T extends object>(
    value: T,
  ): value is Extract<T, Record<At<K, 0>, unknown>> => {
    return keys[0] in value
  }
}

type Foo = { foo1: string, foo2: string }
type Bar = { bar1: string, bar2: string }
type FooBar = Foo | Bar
const barKeys = ['bar1', 'bar2'] as const
export const isBar = mainKeyGuard(barKeys)<FooBar>

πŸ™ Actual behavior

Starting with the v5.7.2, the generated d.ts file declares isBar as a function which reference a missing generic type T, so an error is raised

export declare const isBar: (value: FooBar) => value is Extract<T, Record<At<K, 0>, unknown>>;

πŸ™‚ Expected behavior

I would expect the isBar function d.ts declaration to not reference a missing generic type T.
Previously, the generated d.ts file behaved like the following

export declare const isBar: (value: FooBar) => value is Bar;

Additional information about the issue

The title mentions "instantiation expression" but I'm not totally sure about how the involved feature is actually named

@Andarist
Copy link
Contributor

Bisects to #59282

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 13, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.8.0 milestone Dec 13, 2024
@robpalme
Copy link

cc @dragomirtitian

@Lange11Remco

This comment has been minimized.

@dragomirtitian
Copy link
Contributor

A simpler repro:

function outer<K>(n: K) {
    return function inner<T>(a: [T, K]): a is [T, K] {
        return null!
    }
}

let nr = outer(1)<number>

Playground Link

The above will generate let nr: (a: [number, number]) => a is [T, K]; which seems to indicate T and K are correctly resolved for parameters but not in parameters but not for predicates. Also If the outer function does not have a type parameter everything works (Playground Link).

Looking into it as well.

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 17, 2024
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.

8 participants