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
{{ message }}
Repository navigation
Incorrect type inference for array rest assignment and inability to add annotation #21519
// A *self-contained* demonstration of the problem follows...// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.const[stringA, ...numbers]=['string',1,2];// const [stringA, ...numbers] = <[string, number, number]>['string', 1, 2];// const [stringA, ...numbers]: [string, number[]] = ['string', 1, 2];// const [stringA, ...numbers]: [string, [number, number]] = ['string', 1, 2];
Expected behavior: Expect numbers to be of type number[] rather than (string | number)[].
TS also does not allow annotating as a tuple
Actual behavior: const numbers: (string | number)[]
I have run into this issue too with overload function
// I have tried this but it does not workinterfaceOperatorTupleextendsArray<OperatorFunction<any,any>>{0: OperatorFunction<any,any>;1: OperatorFunction<any,any>;2: OperatorFunction<any,any>;3: OperatorFunction<any,any>;4: OperatorFunction<any,any>;5: OperatorFunction<any,any>;6: OperatorFunction<any,any>;7: OperatorFunction<any,any>;length: 8;}// this is an overload functionexportfunctionSelect<A,B>(mapFn: ((state: A)=>B), ...operators: OperatorFunction<any,any>[]){returnfunction(target: any,name: string): void{if(typeofmapFn!=='function'){thrownewTypeError(`Unexpected type '${typeofmapFn}' in select operator,`+` expected 'function'`);}// trying to cast type tupleoperators=[...operators]asOperatorTuple;constdescriptor=Object.getOwnPropertyDescriptor(target,name);if(deletetarget[name]){Object.defineProperty(target,name,{get(){constsource$=NgrxSelect.store;if(!source$){thrownewError('NgrxSelect not connected to store!');}// I have tried this too but have failedif(operators.length>8){thrownewError()}// Error: Expect 0-9 arguments but received 1 or morereturnsource$.pipe(select(mapFn), ...operators);},
...descriptor});}};}
Activity
mhegazy commentedon Jan 31, 2018
Duplicate of #3369. A tuple type is not inferred in the absence of an explicit type annotation.
you can find related discussions in #16656
theseyi commentedon Feb 1, 2018
Mohamed Hegazy (@mhegazy) so how would the
numbersrest assignment be annotated as a tuple? I believe I tried to this in the last line:mhegazy commentedon Feb 1, 2018
The right type should be
const [stringA, ...numbers]: [string, number, number]. andnumbersshould be inferred correctly. that is a bug we should fix.sandangel commentedon Feb 8, 2018
I have run into this issue too with overload function
ajafff commentedon Jul 27, 2018
The issue in the opening post is fixed in
typescript@next, maybe even3.0.0-rc.numbersis now of typenumber[].RyanCavanaugh commentedon Oct 29, 2025
It's now
[number, number], even better (?)