How to tell TypeScript that I'm returning an array of arrays of the input type?
up vote
0
down vote
favorite
I have a function that chunks arrays:
const chunkArray = (inputArray: any, chunks: number) => {
const chunkedArray = ;
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
export default chunkArray;
I would like my linter to know for a given input array, what the output array looks like. For example for
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
my linter should now, that chunkedArrays is an array of arrays of numbers. Currently it says it's an array of arrays of any.
How can I achieve that?
arrays typescript types typescript-typings typescript-types
add a comment |
up vote
0
down vote
favorite
I have a function that chunks arrays:
const chunkArray = (inputArray: any, chunks: number) => {
const chunkedArray = ;
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
export default chunkArray;
I would like my linter to know for a given input array, what the output array looks like. For example for
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
my linter should now, that chunkedArrays is an array of arrays of numbers. Currently it says it's an array of arrays of any.
How can I achieve that?
arrays typescript types typescript-typings typescript-types
you can add a type signature for function return value(inputArray: any, chunks: number): number => {...}
– SET
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a function that chunks arrays:
const chunkArray = (inputArray: any, chunks: number) => {
const chunkedArray = ;
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
export default chunkArray;
I would like my linter to know for a given input array, what the output array looks like. For example for
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
my linter should now, that chunkedArrays is an array of arrays of numbers. Currently it says it's an array of arrays of any.
How can I achieve that?
arrays typescript types typescript-typings typescript-types
I have a function that chunks arrays:
const chunkArray = (inputArray: any, chunks: number) => {
const chunkedArray = ;
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
export default chunkArray;
I would like my linter to know for a given input array, what the output array looks like. For example for
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
my linter should now, that chunkedArrays is an array of arrays of numbers. Currently it says it's an array of arrays of any.
How can I achieve that?
arrays typescript types typescript-typings typescript-types
arrays typescript types typescript-typings typescript-types
asked yesterday
J. Hesters
429630
429630
you can add a type signature for function return value(inputArray: any, chunks: number): number => {...}
– SET
yesterday
add a comment |
you can add a type signature for function return value(inputArray: any, chunks: number): number => {...}
– SET
yesterday
you can add a type signature for function return value
(inputArray: any, chunks: number): number => {...}– SET
yesterday
you can add a type signature for function return value
(inputArray: any, chunks: number): number => {...}– SET
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You need to add a generic type parameter to your function. The type parameter will capture the actual type of the elements in the input array and then you can use this generic type to specify how it relates to your result:
const chunkArray = <T>(inputArray: T, chunks: number) => {
const chunkedArray: T = ; // use T as the type for chunkedArray
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // number
const chunkedArraysString = chunkArray(["1", "2", "3", "4", "5"], 2); // string
1
Thank you very much!
– J. Hesters
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to add a generic type parameter to your function. The type parameter will capture the actual type of the elements in the input array and then you can use this generic type to specify how it relates to your result:
const chunkArray = <T>(inputArray: T, chunks: number) => {
const chunkedArray: T = ; // use T as the type for chunkedArray
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // number
const chunkedArraysString = chunkArray(["1", "2", "3", "4", "5"], 2); // string
1
Thank you very much!
– J. Hesters
yesterday
add a comment |
up vote
2
down vote
accepted
You need to add a generic type parameter to your function. The type parameter will capture the actual type of the elements in the input array and then you can use this generic type to specify how it relates to your result:
const chunkArray = <T>(inputArray: T, chunks: number) => {
const chunkedArray: T = ; // use T as the type for chunkedArray
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // number
const chunkedArraysString = chunkArray(["1", "2", "3", "4", "5"], 2); // string
1
Thank you very much!
– J. Hesters
yesterday
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to add a generic type parameter to your function. The type parameter will capture the actual type of the elements in the input array and then you can use this generic type to specify how it relates to your result:
const chunkArray = <T>(inputArray: T, chunks: number) => {
const chunkedArray: T = ; // use T as the type for chunkedArray
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // number
const chunkedArraysString = chunkArray(["1", "2", "3", "4", "5"], 2); // string
You need to add a generic type parameter to your function. The type parameter will capture the actual type of the elements in the input array and then you can use this generic type to specify how it relates to your result:
const chunkArray = <T>(inputArray: T, chunks: number) => {
const chunkedArray: T = ; // use T as the type for chunkedArray
let i = 0;
const n = inputArray.length;
while (i < n) {
chunkedArray.push(inputArray.slice(i, (i += chunks)));
}
return chunkedArray;
};
const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // number
const chunkedArraysString = chunkArray(["1", "2", "3", "4", "5"], 2); // string
answered yesterday
Titian Cernicova-Dragomir
50.3k33148
50.3k33148
1
Thank you very much!
– J. Hesters
yesterday
add a comment |
1
Thank you very much!
– J. Hesters
yesterday
1
1
Thank you very much!
– J. Hesters
yesterday
Thank you very much!
– J. Hesters
yesterday
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203409%2fhow-to-tell-typescript-that-im-returning-an-array-of-arrays-of-the-input-type%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
you can add a type signature for function return value
(inputArray: any, chunks: number): number => {...}– SET
yesterday