Skip to content Skip to sidebar Skip to footer

KnockoutObservableArray With Typed Elements In TypeScript

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 defi

Solution 1:

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;

Post a Comment for "KnockoutObservableArray With Typed Elements In TypeScript"