Last active
December 18, 2015 17:19
-
-
Save abergs/5817971 to your computer and use it in GitHub Desktop.
Generic observable example (http://ideasof.andersaberg.com/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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