-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Incorrect type inference for array rest assignment and inability to add annotation #21519
Comments
@mhegazy so how would the const [stringA, ...numbers]: [string, [number, number]] = ['string', 1, 2]; |
The right type should be |
I have run into this issue too with overload function // I have tried this but it does not work
interface OperatorTuple extends Array<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 function
export function Select<A, B>(mapFn: ((state: A) => B), ...operators: OperatorFunction<any, any>[]) {
return function(target: any, name: string): void {
if (typeof mapFn !== 'function') {
throw new TypeError(
`Unexpected type '${typeof mapFn}' in select operator,` + ` expected 'function'`
);
}
// trying to cast type tuple
operators = [...operators] as OperatorTuple;
const descriptor = Object.getOwnPropertyDescriptor(target, name);
if (delete target[name]) {
Object.defineProperty(target, name, {
get() {
const source$ = NgrxSelect.store;
if (!source$) {
throw new Error('NgrxSelect not connected to store!');
}
// I have tried this too but have failed
if (operators.length > 8) {
throw new Error()
}
// Error: Expect 0-9 arguments but received 1 or more
return source$.pipe(select(mapFn), ...operators);
},
...descriptor
});
}
};
} |
The issue in the opening post is fixed in |
TypeScript Version: 2.7.1-insiders.20180127
Search Terms: array destructuring rest assignment
Code
Expected behavior: Expect
numbers
to be of typenumber[]
rather than(string | number)[]
.TS also does not allow annotating as a tuple
Actual behavior: const numbers: (string | number)[]
Playground Link: http://www.typescriptlang.org/play/index.html#src=const%20%5BstringA%2C%20...numbers%5D%20%3D%20%5B'string'%2C%201%2C%202%5D%3B%0A%2F%2F%20const%20%5BstringA%2C%20...numbers%5D%20%3D%20%3C%5Bstring%2C%20number%2C%20number%5D%3E%5B'string'%2C%201%2C%202%5D%3B%0A%2F%2F%20const%20%5BstringA%2C%20...numbers%5D%3A%20%5Bstring%2C%20number%5B%5D%5D%20%3D%20%5B'string'%2C%201%2C%202%5D%3B%0A%2F%2F%20const%20%5BstringA%2C%20...numbers%5D%3A%20%5Bstring%2C%20%5Bnumber%2C%20number%5D%5D%20%3D%20%5B'string'%2C%201%2C%202%5D%3B
Related Issues:
The text was updated successfully, but these errors were encountered: