You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, U is constrained to an object type where properties with string names are constrained to keyof T. However, it is possible for an instantiation to supply a type argument for U that has properties with symbol names which aren't constrained in any way. Therefore U[K] isn't known to be constrained to keyof T.
Changing the constraint of U to an object type where all properties are constrained to keyof T does indeed work:
Yep, so it seems like I was completely wrong on this one. The mistake I made was that the PR I linked to only concerned the case when relating to an indexed access type; this example is about relating from an indexed access type. Subtle, but wrong. Sorry!
Activity
jack-williams commentedon Nov 22, 2018
This might be affected by this: #27490 (and thus working as intended).
Map[K]is no longer pulling in the constraintkeyof Tbecause bothMapandKare generic.I'd wait on a final say from someone on the team though,
pikax commentedon Nov 23, 2018
What I find odd is the intellisense/editor picking up the correct value.
Is there a way to have the same behavior without the error?
jack-williams commentedon Nov 23, 2018
Is your editor using an older version of tsserver?
pikax commentedon Nov 23, 2018
I'm using VSCode and even the typescript playground it works (even with the error it resolves the correct type).
playground
EDIT:

weswigham commentedon Nov 26, 2018
I think @jack-williams is right; but I'm unsure if we wanted this specific kind of pattern to break
ahejlsberg commentedon Nov 26, 2018
This is working as intended. A simplified example:
Here,
Uis constrained to an object type where properties withstringnames are constrained tokeyof T. However, it is possible for an instantiation to supply a type argument forUthat has properties with symbol names which aren't constrained in any way. ThereforeU[K]isn't known to be constrained tokeyof T.Changing the constraint of
Uto an object type where all properties are constrained tokeyof Tdoes indeed work:jack-williams commentedon Nov 26, 2018
Yep, so it seems like I was completely wrong on this one. The mistake I made was that the PR I linked to only concerned the case when relating to an indexed access type; this example is about relating from an indexed access type. Subtle, but wrong. Sorry!
weswigham commentedon Nov 26, 2018
cc @DanielRosenwasser to double-check that we're documenting this break in the 3.2 (? I think that's the first) release.
typescript-bot commentedon Dec 13, 2018
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
pikax commentedon Feb 14, 2019
Sorry to comment on a closed issue, is this suppose to happen:
TestGenericis getting a compile error butTestSpecificdoesn't, shouldn't the type be correct?playground