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
interfaceIFoo{id: string;}interfaceITest<TextendsIFoo|IFoo[]>{data: T;};interfaceITest2<TextendsIFoo|IFoo[]=IFoo>{data: T;}consttest1: ITest={// Generic type 'ITest<T>' requires 1 type argument(s). ts(2314)data: [{id: ''}]}consttest2: ITest2={data: [{// Property 'id' is missing in type '{ id: string; }[]' but required in type 'IFoo'. ts(2741)id: 'fds'}]}
Expected behavior:
I expect the interface ITest to understand that data is typeof Array<IFoo> like for generic method inference.
If I precise a default value for generic parameter (ITest2), I can understand that in my test2 implementation, it will take IFoo as a default implementation, even if I found the error explanation a little bit weird to understand.
Actual behavior:
As describe in the code section, I get some errors because I must provide the expected type.
The thing is, sometime, in some third party library definition, they don't export some type, so I cannot easily pick the expected variant of the generic type I want to have.
A way to fix it, is to recreate a type by mimicking the structure of the original type, but I increase the number of source of truth, so as soon as the third party update the definition, it breaks my duplicated type.
Still would really love to see this for defining plain objects that map to a generic type! Would be really helpful for creating inputs that a framework expects without having to use extra factory functions everywhere.
TypeScript Version: 3.4.0-dev.20190226
Search Terms:
Generic parameters inference
Code
Expected behavior:
I expect the interface
ITest
to understand thatdata
is typeofArray<IFoo>
like for generic method inference.If I precise a default value for generic parameter (
ITest2
), I can understand that in mytest2
implementation, it will takeIFoo
as a default implementation, even if I found the error explanation a little bit weird to understand.Actual behavior:
As describe in the
code
section, I get some errors because I must provide the expected type.The thing is, sometime, in some third party library definition, they don't export some
type
, so I cannot easily pick the expected variant of the generic type I want to have.A way to fix it, is to recreate a
type
by mimicking the structure of the originaltype
, but I increase the number of source of truth, so as soon as the third party update the definition, it breaks my duplicatedtype
.Playground Link:
Playground link
Related Issues:
Well... I found lot of issues but related with generic method (that works like a charm today), not really just for interface...
The text was updated successfully, but these errors were encountered: