Reputation: 19718
I'm currently looking to migrate our project to TypeScript. I've found this great set of definition files and I'm currently experimenting with the one for Knockout.
I know the definition file has a type for the observableArray KnockoutObservableArray
and I also am aware you can define a typed array like MyType[]
.
I'd like to know if I can somehow combine these two? I'd like to create a KnockoutObservableArray
for which the elements should be of type MyType
.
Thanks in advance!
Upvotes: 2
Views: 1867
Reputation: 250812
The road-map for TypeScript includes generics, which I think is what you need in order to create what you want. The following code isn't real and may not even be how the TypeScript team implement generics, but it gives a flavour of how I think it would be implemented. I have also left out implementation details on how to make it observable etc:
class KnockoutObservableArray <T> {
constructor(public Items: T[]) {
}
}
var observableString = new KnockoutObservableArray<string>(['foo', 'bar']);
But as I mentioned, generics aren't yet included in TypeScript, so for now you'll have to make it dynamic!
var observableString: any;
Upvotes: 4