Typescript generic interface parameter assignment
up vote
2
down vote
favorite
I’m struggling to understand why this example isn’t considered valid by the typescript compiler:
interface IExample<T> {
param: T
}
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam };
}
The error produced is:
Type 'U' is not assignable to type 'I["param"]'.
My (assumedly incorrect) reading of this snippet is:
IExample<T>expectsparamto have typeT.
Iis a subtype ofIExample<U>, meaningparamhas typeU.
myParamhas typeUfrom the parameter annotation.- Therefore
myParamshould be a valid value forparamofI.
Prefixing the return value with <I> clears the error, so why does the error appear in the first place?
typescript generics
add a comment |
up vote
2
down vote
favorite
I’m struggling to understand why this example isn’t considered valid by the typescript compiler:
interface IExample<T> {
param: T
}
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam };
}
The error produced is:
Type 'U' is not assignable to type 'I["param"]'.
My (assumedly incorrect) reading of this snippet is:
IExample<T>expectsparamto have typeT.
Iis a subtype ofIExample<U>, meaningparamhas typeU.
myParamhas typeUfrom the parameter annotation.- Therefore
myParamshould be a valid value forparamofI.
Prefixing the return value with <I> clears the error, so why does the error appear in the first place?
typescript generics
1
I would have expected a different error message: something like{ param: U }isn't assignable to generic typeI: I is an unknown type, which could have many more attributes and methods than just param. So{ param: myParam }is almost guaranteed to not be of type I.
– JB Nizet
Nov 12 at 17:38
Found a duplicate which I missed before posting: stackoverflow.com/q/40690797/1813169
– MTCoster
Nov 12 at 17:54
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I’m struggling to understand why this example isn’t considered valid by the typescript compiler:
interface IExample<T> {
param: T
}
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam };
}
The error produced is:
Type 'U' is not assignable to type 'I["param"]'.
My (assumedly incorrect) reading of this snippet is:
IExample<T>expectsparamto have typeT.
Iis a subtype ofIExample<U>, meaningparamhas typeU.
myParamhas typeUfrom the parameter annotation.- Therefore
myParamshould be a valid value forparamofI.
Prefixing the return value with <I> clears the error, so why does the error appear in the first place?
typescript generics
I’m struggling to understand why this example isn’t considered valid by the typescript compiler:
interface IExample<T> {
param: T
}
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam };
}
The error produced is:
Type 'U' is not assignable to type 'I["param"]'.
My (assumedly incorrect) reading of this snippet is:
IExample<T>expectsparamto have typeT.
Iis a subtype ofIExample<U>, meaningparamhas typeU.
myParamhas typeUfrom the parameter annotation.- Therefore
myParamshould be a valid value forparamofI.
Prefixing the return value with <I> clears the error, so why does the error appear in the first place?
typescript generics
typescript generics
asked Nov 12 at 17:31
MTCoster
2,07421736
2,07421736
1
I would have expected a different error message: something like{ param: U }isn't assignable to generic typeI: I is an unknown type, which could have many more attributes and methods than just param. So{ param: myParam }is almost guaranteed to not be of type I.
– JB Nizet
Nov 12 at 17:38
Found a duplicate which I missed before posting: stackoverflow.com/q/40690797/1813169
– MTCoster
Nov 12 at 17:54
add a comment |
1
I would have expected a different error message: something like{ param: U }isn't assignable to generic typeI: I is an unknown type, which could have many more attributes and methods than just param. So{ param: myParam }is almost guaranteed to not be of type I.
– JB Nizet
Nov 12 at 17:38
Found a duplicate which I missed before posting: stackoverflow.com/q/40690797/1813169
– MTCoster
Nov 12 at 17:54
1
1
I would have expected a different error message: something like
{ param: U } isn't assignable to generic type I: I is an unknown type, which could have many more attributes and methods than just param. So { param: myParam } is almost guaranteed to not be of type I.– JB Nizet
Nov 12 at 17:38
I would have expected a different error message: something like
{ param: U } isn't assignable to generic type I: I is an unknown type, which could have many more attributes and methods than just param. So { param: myParam } is almost guaranteed to not be of type I.– JB Nizet
Nov 12 at 17:38
Found a duplicate which I missed before posting: stackoverflow.com/q/40690797/1813169
– MTCoster
Nov 12 at 17:54
Found a duplicate which I missed before posting: stackoverflow.com/q/40690797/1813169
– MTCoster
Nov 12 at 17:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The problem is that I might have other required properties in addition to param. Typescript will force your generic function implementation to return a valid value for any I that satisfies the constraint of extending IExample<U>, and it does not.
For example:
interface DerivedIExample extends IExample<number> {
other : string
}
let o = testFunc<number, DerivedIExample>(0)
o.other // required by the DerivedIExample but not assigned
In your simple example, it would be best to do away with I completely:
function testFunc<U>(myParam: U): IExample<U> {
return { param: myParam };
}
You can force the compiler to accept your code using a type assertion but as outlined above that is not type-safe:
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam } as I; // NOT TYPE SAFE! USE WITH CARE !
}
I don’t know why I didn’t just eliminateIin the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?
– MTCoster
Nov 12 at 17:52
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
2
It turns out the error has already been fixed in the nightly builds, it’s now:Type '{ param: U; }' is not assignable to type 'I'.
– MTCoster
Nov 12 at 18:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The problem is that I might have other required properties in addition to param. Typescript will force your generic function implementation to return a valid value for any I that satisfies the constraint of extending IExample<U>, and it does not.
For example:
interface DerivedIExample extends IExample<number> {
other : string
}
let o = testFunc<number, DerivedIExample>(0)
o.other // required by the DerivedIExample but not assigned
In your simple example, it would be best to do away with I completely:
function testFunc<U>(myParam: U): IExample<U> {
return { param: myParam };
}
You can force the compiler to accept your code using a type assertion but as outlined above that is not type-safe:
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam } as I; // NOT TYPE SAFE! USE WITH CARE !
}
I don’t know why I didn’t just eliminateIin the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?
– MTCoster
Nov 12 at 17:52
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
2
It turns out the error has already been fixed in the nightly builds, it’s now:Type '{ param: U; }' is not assignable to type 'I'.
– MTCoster
Nov 12 at 18:08
add a comment |
up vote
1
down vote
accepted
The problem is that I might have other required properties in addition to param. Typescript will force your generic function implementation to return a valid value for any I that satisfies the constraint of extending IExample<U>, and it does not.
For example:
interface DerivedIExample extends IExample<number> {
other : string
}
let o = testFunc<number, DerivedIExample>(0)
o.other // required by the DerivedIExample but not assigned
In your simple example, it would be best to do away with I completely:
function testFunc<U>(myParam: U): IExample<U> {
return { param: myParam };
}
You can force the compiler to accept your code using a type assertion but as outlined above that is not type-safe:
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam } as I; // NOT TYPE SAFE! USE WITH CARE !
}
I don’t know why I didn’t just eliminateIin the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?
– MTCoster
Nov 12 at 17:52
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
2
It turns out the error has already been fixed in the nightly builds, it’s now:Type '{ param: U; }' is not assignable to type 'I'.
– MTCoster
Nov 12 at 18:08
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The problem is that I might have other required properties in addition to param. Typescript will force your generic function implementation to return a valid value for any I that satisfies the constraint of extending IExample<U>, and it does not.
For example:
interface DerivedIExample extends IExample<number> {
other : string
}
let o = testFunc<number, DerivedIExample>(0)
o.other // required by the DerivedIExample but not assigned
In your simple example, it would be best to do away with I completely:
function testFunc<U>(myParam: U): IExample<U> {
return { param: myParam };
}
You can force the compiler to accept your code using a type assertion but as outlined above that is not type-safe:
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam } as I; // NOT TYPE SAFE! USE WITH CARE !
}
The problem is that I might have other required properties in addition to param. Typescript will force your generic function implementation to return a valid value for any I that satisfies the constraint of extending IExample<U>, and it does not.
For example:
interface DerivedIExample extends IExample<number> {
other : string
}
let o = testFunc<number, DerivedIExample>(0)
o.other // required by the DerivedIExample but not assigned
In your simple example, it would be best to do away with I completely:
function testFunc<U>(myParam: U): IExample<U> {
return { param: myParam };
}
You can force the compiler to accept your code using a type assertion but as outlined above that is not type-safe:
function testFunc<U, I extends IExample<U>>(myParam: U): I {
return { param: myParam } as I; // NOT TYPE SAFE! USE WITH CARE !
}
answered Nov 12 at 17:38
Titian Cernicova-Dragomir
51.5k33148
51.5k33148
I don’t know why I didn’t just eliminateIin the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?
– MTCoster
Nov 12 at 17:52
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
2
It turns out the error has already been fixed in the nightly builds, it’s now:Type '{ param: U; }' is not assignable to type 'I'.
– MTCoster
Nov 12 at 18:08
add a comment |
I don’t know why I didn’t just eliminateIin the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?
– MTCoster
Nov 12 at 17:52
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
2
It turns out the error has already been fixed in the nightly builds, it’s now:Type '{ param: U; }' is not assignable to type 'I'.
– MTCoster
Nov 12 at 18:08
I don’t know why I didn’t just eliminate
I in the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?– MTCoster
Nov 12 at 17:52
I don’t know why I didn’t just eliminate
I in the first place, thanks! Is it worth opening an issue to note the unhelpful(/misleading?) error message?– MTCoster
Nov 12 at 17:52
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
@MTCoster your call, you could ... the message is misleading ...
– Titian Cernicova-Dragomir
Nov 12 at 17:54
2
2
It turns out the error has already been fixed in the nightly builds, it’s now:
Type '{ param: U; }' is not assignable to type 'I'.– MTCoster
Nov 12 at 18:08
It turns out the error has already been fixed in the nightly builds, it’s now:
Type '{ param: U; }' is not assignable to type 'I'.– MTCoster
Nov 12 at 18:08
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267269%2ftypescript-generic-interface-parameter-assignment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I would have expected a different error message: something like
{ param: U }isn't assignable to generic typeI: I is an unknown type, which could have many more attributes and methods than just param. So{ param: myParam }is almost guaranteed to not be of type I.– JB Nizet
Nov 12 at 17:38
Found a duplicate which I missed before posting: stackoverflow.com/q/40690797/1813169
– MTCoster
Nov 12 at 17:54