TypeScript - best syntax for adding new properties to a typed object without declaring a new interface? -
i've been using typescript angular half year now. when need add $scope, follow imho best practice of defining interface this:
interface imyscope extends ng.iscope { myaddedprop:string; } ... link:($scope:imyscope ){ $scope.myaddedprop = "foo"; }
however, @ times still convenient add properties $scope on fly (mainly when i'm prototyping). cases this:
link:($scope:iscope ){ (<any>$scope).myaddedprop = "foo"; //or $scope["myaddedprop"] = "foo"; //if have add many props, use this: var $scopea: = $scope; $scopea.myaddedprop1 = "foo"; $scopea.myaddedprop2 = "bar"; $scopea.myaddedprop3 = "baz"; }
i've had problem in nodejs , other libraries - when need add few properties , don't want bother interface.
so search gives options, isn't there better way it?
$scope*.newprop = "foo";
?
if put class on scope:
interface imyscope extends ng.iscope { vm:someclass; } link:($scope:imyscope){ scope.vm = new someclass(); }
then can add new properties someclass
without need maintain interface manually.
even better use controller
option on directive setup class you.
Comments
Post a Comment