Skip to content

Generic mapped as clause regression in 4.7 #49541

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
samhh opened this issue Jun 14, 2022 · 4 comments
Closed

Generic mapped as clause regression in 4.7 #49541

samhh opened this issue Jun 14, 2022 · 4 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@samhh
Copy link

samhh commented Jun 14, 2022

Bug Report

πŸ”Ž Search Terms

mapped, generic, as clause

πŸ•— Version & Regression Information

  • This changed between versions 4.6.4 and 4.7.2/4.7.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

The issue goes away if you remove the as Id<A> in Obj.

type Id<A> = A;

type Obj<A extends string> = {
  [K in A as Id<A>]: null
}

declare const getObj: <A extends string>() => Obj<A>

const f = <A extends 'a'>(): null => {
  const o = getObj<A>()
  return o['a' as A]
}

πŸ™ Actual behavior

1. Type 'Obj<A>[A]' is not assignable to type 'null'.
     Type 'Obj<A>["a"]' is not assignable to type 'null'.

πŸ™‚ Expected behavior

The compiler used to resolve o['a' as A] as null.

@nicolas377
Copy link
Contributor

nicolas377 commented Jul 23, 2022

git bisecting locally off this repro landed the first bad commit at 8c060ee (#47889, #47794).

@nicolas377
Copy link
Contributor

I also narrowed down to this line in the PR's diff. What to do with it, I'm unsure of, but I thought it might be of use.

@RyanCavanaugh RyanCavanaugh removed their assignment Jul 28, 2022
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 29, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.9.0 milestone Jul 29, 2022
@ahejlsberg
Copy link
Member

This is working as intended. The commits above added checks that should have been present in the first place. The issue we have is that when a mapped type includes an as clause, we can't consistently reason about the constraint of that mapped type. In the particular example here it is obvious that the constraint is null because the as clause specifies an identity mapping and the template type null doesn't reference the iteration type K. However, in most cases the template type does reference the iteration type, and often it isn't even possible to construct a reverse mapping for the as clause type.

Also see comment here.

@ahejlsberg ahejlsberg added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Investigation This issue needs a team member to investigate its status. labels Aug 4, 2022
@ahejlsberg ahejlsberg removed this from the TypeScript 4.9.0 milestone Aug 4, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants