-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Type assignable to index access of constraint of type parameter is incorrectly assignable to the index access itself #46076
Comments
here is a more minimal example: //no error
const impostor = <T extends [string|number]>(): T[0] => 'i am string'
const a: number = impostor<[number]>() // actually is string
a.toExponential() //runtime error |
So, I think this is a known design limitation in the way we check assignability in our type system. As to workarounds, I think you're right: this assignability rule only applies for an indexed access We have made this assignability rule more strict over time, like here and here, but for this specific case pointed out here, I don't see how we could make the rule not applicable, since the index type in the examples is concrete ( |
I don't think this is a limitation, but rather a correct feature.
It is a good thing that return value is not constrained in the way the OP suggests. |
@craigphicks i don't get what would make an error there any less user friendly than in the original |
@Detach head - A more slim example
I am saying functional access to members is a common way to allow transformations of the returned member. It's a common software pattern. A common software pattern shouldn't trigger an error. If your example is rewritten as:
an error is produced. I do agree that
should produce an error for the same reason that |
Bug Report
π Search Terms
generic subtype, generic constraint
π Version & Regression Information
β― Playground Link
(strict settings)
Playground link with relevant code
π» Code
π Actual behavior
No error but there should have been.
π Expected behavior
An error on the return statement of getItemAndCheck().
Use case
A rest api which returns data with very similar top level structure, but with differing nested properties, and wanting to write generic handlers for all responses.
Workaround
One could make the properties of the generic themselves generic type parameters as well, but this becomes more difficult the more complex the types are or if you want to manipulate several different properties in a generic way instead of one.
The text was updated successfully, but these errors were encountered: