Prohibited attributes in a Typescript Interface -


i create typescript interface not requires particular attributes present, prohibits attributes not part of definition. here example:

  export interface icomponentdirective {     scope : any;     templateurl : string;   };    var ddo : icomponentdirective = {     scope: {       dt: '='     },     templateurl: 'directives.datepicker',     controller: function() {       console.log('hello world');     }   }; 

even though controller not defined in interface, ddo assignment doesn't throw error. doing research, looks might designed:

notice our object has more properties this, compiler checks @ least ones required present , match types required.

http://www.typescriptlang.org/handbook#interfaces

notice, however, after declare ddo icomponentdirective, if try like:

ddo.transclude = false; 

the transpiler complain with:

2339 property 'transclude' not exist on type 'icomponentdirective'.

is there way enforce tighter contract?

in short (but depending on definition of "tighter") can't restrict things beyond this.

the interface contract says "if these members not present, aren't 1 of these". if object happens have additional members, that's fine; when using interface, invisible you.

for example (based on code), when type ddo. suggest members on interface, because have told compiler use interface type.

you can't use interface prevent member being defined. can't think of language off top of head. example, in c# can implement more interface requires implement, when using interface type, other members not available.

adding properties dynamically

the question why ddo.tranclude = false; generates warning bit different. isn't related interfaces - when there no interface:

  var ddo = {     scope: {       dt: '='     },     templateurl: 'directives.datepicker',     controller: function() {       console.log('hello world');     }   };    ddo.transclude = false; // nope 

the reason is... point of typescript! warning you might have typed transclude incorrectly. perhaps meant templateurl, does exist. if typescript didn't warn kind of problem, allow introduce typographical bugs code.

so typescript generate type object create , enforce structure, unless tell otherwise.

if wanted there "sometimes transclude member" can make possible:

interface sometimestransclude {     scope: { dt: string};     templateurl: string;     controller: () => void;     transclude?: boolean; }    var ddo: sometimestransclude = {     scope: {       dt: '='     },     templateurl: 'directives.datepicker',     controller: function() {       console.log('hello world');     }   };    ddo.transclude = false; 

or can dance straight past compiler (at own risk) using:

ddo['transclude'] = false; 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -