Issue
After the update from the angular 5.1 to 6.1 I stared to receive some error from my code like the following:
Error: ngc compilation failed: components/forms/utils.ts(5,3): error TS2322: Type '[number] | [number, number, number, number]' is not assignable to type '[number]'.
Follows the code:
export function bsColumnClass(sizes: [number]) {
let sizebs = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-',];
sizes = sizes || [12, 12, 12, 12];
let className = sizes.map(function callback(value, index, array) {
return sizebs[index].concat(value.toString());
}).join(" ");
return className;
}
I notice that the problem is because I have a function parameter sizes: [number]
and after that I'm traing to do this line of code: sizes = sizes || [12, 12, 12, 12];
Can you tell me the better way to solve this?
Solution
If you want to define an array of numbers the syntax is number[]
(and in general for arrays we have type[]
). What you defined there is a tuple type, which has a fixed number of elements and can have heterogeneous element types.
export function bsColumnClass(sizes: number[]) {
}
Or if you want to mandate the caller pass exactly 4 elements in the array, which appears to make sense in this case as sizebs
is fixed, you can use the tuple type:
export function bsColumnClass(sizes: [number, number, number, number]) {
let sizebs = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-',];
sizes = sizes || [12, 12, 12, 12];
let className = sizes.map(function callback(value, index, array) {
return sizebs[index].concat(value.toString());
}).join(" ");
return className;
}
Answered By - Titian Cernicova-Dragomir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.