Closed
Description
Bug Report
π Search Terms
generic subtype, generic constraint
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic subtypes
β― Playground Link
(strict settings)
Playground link with relevant code
π» Code
interface ContainerBase {
success: boolean;
item: {
itemId: string;
};
}
interface MyContainer {
success: boolean;
item: {
itemId: string;
thing: {
color: string;
size: number;
};
}
}
function getItemAndCheck<ContainerT extends ContainerBase>(container: ContainerT): ContainerT["item"] {
if (!container.success) {
throw new Error(`container success fail`);
}
//return container.item; // good code
return {itemId: "id"}; // bad code, but tsc raises no error.
// The error could be (for example): '{itemId: "id"}' is assignable to the constraint of type 'ContainerT["item"]',
// but 'ContainerT["item"]' could be instantiated with a different subtype of constraint '{itemId: "id"}'
// (like existing errors about generic subtypes)
}
const item = getItemAndCheck(fetchMyContainer());
console.log(item.thing.color); // throws an error at runtime but tsc raised no error earlier
// the caller thinks 'item' is the property of the specific subtype MyContainer but inside
// getItemAndCheck() you can return any subtype not just that one.
function fetchMyContainer(): MyContainer {
// example
return {
success: true,
item: {
thing: {
color: "green",
size: 42,
},
itemId: "id",
}
}
}
π 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
[-]Inconsistency between function body and caller regarding subtypes of a property of a generic[/-][+]Type assignable to constraint of type parameter is incorrectly assignable to the parameter as well[/+][-]Type assignable to constraint of type parameter is incorrectly assignable to the parameter as well[/-][+]Type assignable to index access of constraint of type parameter is incorrectly assignable to the index access itself[/+]DetachHead commentedon Apr 2, 2022
here is a more minimal example:
gabritto commentedon Apr 6, 2022
So, I think this is a known design limitation in the way we check assignability in our type system.
To clarify: the reason there's no error on the original example is that when we check if the type of the return
{ itemId: "id" }
is assignable to the annotated return typeContainerT["item"]
, we check if{ itemId: "id" }
is assignable to the constraint ofContainerT["item"]
, which isContainerBase["item"]
={ itemId: string }
, so we conclude it is assignable.The general rule is this: "A type S is related to a type T[K] if S is related to C, where C is the base constraint of T[K] for writing" (implemented here: https://github.dev/microsoft/TypeScript/blob/3fd8a6e44341f14681aa9d303dc380020ccb2147/src/compiler/checker.ts#L19368).
The rule is knowingly unsound, and it is unfortunate that we don't error on cases like the above, but the rule is useful in ways explained in this comment.
As to workarounds, I think you're right: this assignability rule only applies for an indexed access
T[K]
if the constraints ofT
andK
are not themselves generic, so to evade this rule, you'd have to make sure either the constraint ofK
is generic, or the constraint ofT
is generic. The result would probably be something less ergonomic than the original code π.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 (
"item"
and0
in the example programs, respectively), which is exactly the case pointed out that we want to support (equivalent to e.g.this["xxx"]
).craigphicks commentedon Jan 9, 2024
I don't think this is a limitation, but rather a correct feature.
It is similar the way that JS getters are intended allow the user to massage values - although getters are not the issue here, the need is similar. Consider this:
It is a good thing that return value is not constrained in the way the OP suggests.
DetachHead commentedon Jan 10, 2024
@craigphicks i don't get what would make an error there any less user friendly than in the original
getItemAndCheck
function from the OP. do you mean ifcontainer.item.itemId.toLocaleUpperCase()
was an error? because i don't see why it would be, sinceContainerT["item"]
will always haveitemId
craigphicks commentedon Jan 10, 2024
@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.
I understand your counterargument that
string
could have been used instead ofContainerT["item"]["ItemId"]
, and agree to disagree whether such usage should error.If your example is rewritten as:
an error is produced. I do agree that
should produce an error for the same reason that
imposter1
does.