-
Notifications
You must be signed in to change notification settings - Fork 187
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
Issue with resolving generic type in Typescript #164
Comments
I also had |
I suspect this is due to the generic, when I change all instances of |
The OP's example does not look like a valid spec since it's missing a interface A {a: number; b: number};
function foo<T extends A>(v: T): T {
return update<T>(v, {a: {$set: 10}});
} The big conditional type in the first clause of the union in the I'm pretty new to typescript and don't fully grok where they try to be sound and where they don't, but I don't know how you'd model this properly without a cascade of variance issues. Updating We ended up working around it with type assertions, but I suppose specifically for discriminated unions you can make it generic across the discriminant instead, so the union that's passed to interface A {
kind: 'A';
a: number;
}
interface B {
kind: 'B';
a: number;
b: number;
}
type Foo = A | B;
// Generic on the discriminant instead of the full object type:
function foo<Kind extends 'A' | 'B'>(v: Foo & {kind: Kind}): Foo & {kind: Kind} {
return update(v, {a: {$set: 10}});
} |
Closing for now, feel free to reopen if more action can be done here |
immutability-helper version: 3.0.2
Function updates a parent (id) of some entity. The entity can be
Group
orTask
. Both of these entities have attributeinGroup: string
. However, when I tried to define these entities like generics, a typescript error was thrown.Query
Error:
Both GroupsMap and TasksMap are maps such as:
Both
Task
aGroup
has attributeinGroup: string
.When I defined update query like this, there was no error, but I lost what type would be returned from the function, which would be very helpful in my case:
Appreciate any help. Thanks
The text was updated successfully, but these errors were encountered: