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?










share|improve this question






















  • you can add a type signature for function return value (inputArray: any, chunks: number): number => {...}
    – SET
    yesterday















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?










share|improve this question






















  • you can add a type signature for function return value (inputArray: any, chunks: number): number => {...}
    – SET
    yesterday













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









J. Hesters

429630




429630












  • 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




you can add a type signature for function return value (inputArray: any, chunks: number): number => {...}
– SET
yesterday












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





share|improve this answer

















  • 1




    Thank you very much!
    – J. Hesters
    yesterday











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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





share|improve this answer

















  • 1




    Thank you very much!
    – J. Hesters
    yesterday















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





share|improve this answer

















  • 1




    Thank you very much!
    – J. Hesters
    yesterday













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Titian Cernicova-Dragomir

50.3k33148




50.3k33148








  • 1




    Thank you very much!
    – J. Hesters
    yesterday














  • 1




    Thank you very much!
    – J. Hesters
    yesterday








1




1




Thank you very much!
– J. Hesters
yesterday




Thank you very much!
– J. Hesters
yesterday


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Landwehr

Reims

Schenkenzell