You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When using the native method 'defineProperty ' to define a 'Getter' for a class, the compiler error! #14650
// a function used to define targetObject's descriptor from SourceObject's propertiesfunctiondefineGetter(SourceObject,targetObject){Object.keys(SourceObject).forEach(function(key){Object.defineProperty(targetObject.prototype,key,{get: function(){returnSourceObject[key]},enumerable: true,configurable: true})})}interfaceIForm{name: string;label: string;value: any;sequence: number;width: number;type: number;}classForm{constructor(form: IForm){defineGetter(form,Form);}getinfo(){return`name is ${this.name}, label is ${this.label}`;}}letform=newForm({name: '1',label: '2',value: 3,sequence: 4,width: 5,type: 6})
Expected behavior & Actual behavior:
I want to complete my definition of Getter with metadata(a Singleton Object) through a defineGetter () method, It seems to be successful. but, When I manually define another Getter (info), the compiler prompts:
“ error TS2339: Property 'name' does not exist on type 'Form'”
“ error TS2339: Property 'type' does not exist on type 'Form'. ”
I just want to use a function to solve my burden of writing the getter repeatedly,How can I solve this problem?
classForm{private_form: IForm;constructor(form: IForm){this._form=form;}getname(){returnthis._form.name;}getlabel(){returnthis._form.name;}getvalue(){returnthis._form.value;}getwidth(){returnthis._form.width;}gettype(){returnthis._form.type;}getsequence(){returnthis._form.sequence;}// extra gettergetinfo(){returnthis.width+this.type;}}
classForm{constructor(form: IForm){defineGetter(form,Form);}getinfo(){return`name is ${this.name}, label is ${this.label}`;}}interfaceFormextendsIForm{}
Can the name of interface and class be the same name?
It takes advantage of what TypeScript calls "interface merging". When an interface and a class have the same name, it automatically merges the two. This is in part why it is often recommended not to name your interfaces with an I... which is a convention in many other languages, since there some "automagic" that can happen when the names are the same.
It is best to think as the type system in TypeScript as an "overlay" on top of the underlying JavaScript.
Also to properly type your interface, you should be marking properties that only have a getter as readonly in the interface, which will disallow assignment to the properties within the type system, which would more accurately reflect your runtime intent.
Activity
ulivz commentedon Mar 14, 2017
How can I only write a class in this way 😐:
RyanCavanaugh commentedon Mar 14, 2017
Write this:
ulivz commentedon Mar 15, 2017
Can the name of interface and class be the same name? well, I didn‘t write it before. Just wrote a simple
interface inheritancekitsonk commentedon Mar 15, 2017
It takes advantage of what TypeScript calls "interface merging". When an interface and a class have the same name, it automatically merges the two. This is in part why it is often recommended not to name your interfaces with an
I...which is a convention in many other languages, since there some "automagic" that can happen when the names are the same.It is best to think as the type system in TypeScript as an "overlay" on top of the underlying JavaScript.
Also to properly type your interface, you should be marking properties that only have a getter as
readonlyin the interface, which will disallow assignment to the properties within the type system, which would more accurately reflect your runtime intent.