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

Narrowing of generic type parameter index #48996

Closed
Fryuni opened this issue May 6, 2022 · 2 comments
Closed

Narrowing of generic type parameter index #48996

Fryuni opened this issue May 6, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Fryuni
Copy link

Fryuni commented May 6, 2022

Bug Report

πŸ”Ž Search Terms

narrowing generic parameter

πŸ•— Version & Regression Information

  • This changed between versions 4.4.3 and 4.6.4

⏯ Playground Link

Playground link with relevant code
Changing the version to 4.4.4 works

πŸ’» Code

Reduced example, the code on the playground is an actual function from our codebase that used to work fine.

type TimestampColumns<T> = {
    [K in keyof T]: T[K] extends Date ? K : never
}[keyof T];

function getTimestamp<Row extends Record<string, unknown>, K extends TimestampColumns<Row>>(
  row: Row,
  timestampColumn: K,
): number {
  const date: Date = row[timestampColumn]; // This used to work;
  return date.valueOf();
}

πŸ™ Actual behavior

The code should compile with no warnings or errors.

πŸ™‚ Expected behavior

Fails with error Type 'Row[K]' is not assignable to type 'Date'.

@Fryuni Fryuni changed the title Narrowing of struct Narrowing of generic type parameter May 6, 2022
@Fryuni Fryuni changed the title Narrowing of generic type parameter Narrowing of generic type parameter index May 6, 2022
@RyanCavanaugh
Copy link
Member

This used to "work" because it was actually completely broken, e.g. in 4.4 you can also write without error:

   const date: number = row[timestampColumn];

See #48992 for a possible feature to address this

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 6, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants