Skip to content

Instantly share code, notes, and snippets.

@abergs
Last active December 18, 2015 17:19
Show Gist options
  • Save abergs/5817971 to your computer and use it in GitHub Desktop.
Save abergs/5817971 to your computer and use it in GitHub Desktop.
Generic observable example (http://ideasof.andersaberg.com/)
/// <reference path="ko.d.ts" />
class MyKnockOutApp {
User: KnockoutObservable<User>;
PrivateMessage = ko.observable<Message>(null);
MyComputedMessage: KnockoutComputed<Message>;
constructor() {
// Will throw an error since types are not matching
this.User = ko.observable<Message>(undefined);
// Works
this.User = ko.observable<User>(undefined);
this.User(new User());
// Read the values. Using the wrong type will throw compiler error
var value: User = this.User();
var value2: Message = this.PrivateMessage();
// Computed example
this.MyComputedMessage = ko.computed<Message>(() => {
// Will throw compiler error
return this.User();
// Works
return this.PrivateMessage();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment