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
interfaceMutableArrayLike<T>{readonlylength: number;[n: number]: T;}typeArrayOfNumber=MutableArrayLike<number>;typeArrayOfConstNumber=ArrayLike<number>;// has readonly subindexinterfacevec3{// the `out` array is modified and then returned as the function resultsubtract<TextendsArrayOfNumber>(out: T,a: ArrayOfConstNumber,b: ArrayOfConstNumber): T;}// later..constresult=vec3.subtract([],vA,vB);
Expected behavior:
Param out and the result are of type number[] or some looser type.
Actual behavior:
Param out and the result are of type never[]
Comments
strictNullChecks is on.
I realise that the compiler gets no help from an empty array parameter to deduce the type of its contents, but I was hoping my restricting of type T would either make it force the the type or throw an error. Making it a never[] seems a bit useless though I can intuit some of the reasoning of the compiler.
The interface works like this as the vec3 interface (from gl-matrix) deals with simple arrays or typed arrays so I typed everything to at least force numbers in there, but I was hoping that I could get it to set the result type to the type of the argument that I pass into the out param, because that is the value that is returned to facilitate easier chaining.
This may be a design limitation but I can always hope.
The text was updated successfully, but these errors were encountered:
We infer never[] from the empty array literal. That is the most specific type possible and it is a subtype of the ArrayOfNumber constraint, so it seems entirely reasonable. It looks to me like your intent is to return some array-like type, but always with an element type of number. I can't think of any way to express that other than overloads:
interfacevec3{// the `out` array is modified and then returned as the function resultsubtract(out: number[],a: ArrayOfConstNumber,b: ArrayOfConstNumber): number[];subtract(out: ArrayOfNumber,a: ArrayOfConstNumber,b: ArrayOfConstNumber): ArrayOfNumber;}
TypeScript Version: 2.0.3
Code
Expected behavior:
Param
out
and the result are of typenumber[]
or some looser type.Actual behavior:
Param
out
and the result are of typenever[]
Comments
strictNullChecks is on.
I realise that the compiler gets no help from an empty array parameter to deduce the type of its contents, but I was hoping my restricting of type T would either make it force the the type or throw an error. Making it a
never[]
seems a bit useless though I can intuit some of the reasoning of the compiler.The interface works like this as the vec3 interface (from gl-matrix) deals with simple arrays or typed arrays so I typed everything to at least force numbers in there, but I was hoping that I could get it to set the result type to the type of the argument that I pass into the
out
param, because that is the value that is returned to facilitate easier chaining.This may be a design limitation but I can always hope.
The text was updated successfully, but these errors were encountered: