Reputation: 2917
Since updating Typescript to 3.x I can not compile this class anymore:
class MyClass<T>{
private myMethods: [(val: T) => void] = [] as [(val: T) => void];
}
More specific, it is [] as [(val: T) => void]
that triggers the error and the compiler message is:
Error:(7, 47) TS2352: Type '[]' cannot be converted to type '[(val: T) => void]'.
Property '0' is missing in type '[]'.
It DO work in Typescript 2.9.2 though!
So what is the problem here and how do I solve it?
Upvotes: 0
Views: 72
Reputation: 15106
If myMethods
is not a tuple but an array (as the name suggests,) you can just do
class MyClass<T>{
private myMethods: Array<(val: T) => void> = [];
}
Upvotes: 1
Reputation: 39916
This is due to tuple length enforcement in TypeScript 3.0 which forces signature [T]
to have one element in the array of type T
.
The only solution is to make an element type optional as follow.
class MyClass<T>{
private myMethods: [((val: T) => void)?] = [] as [((val: T) => void)?];
}
Upvotes: 1