microsoft / microsoft/TypeScript

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

Open
#60,758 4 comments 0 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Dec 13, 2024.

Bug Domain: Declaration Emit
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.