The correct way to include a JSON object in a typescript typed variable using a class?

This is a simpler example of what I'm trying to do:

export class Person{ id:Number; name:String; } export class PersonForm{ // This line: default:Person = {name: "Guy"}; // Gives the following error: // Error:(25, 5) TS2322: Type '{ name: string; }' is not assignable to type 'Person'. // Property 'id' is missing in type '{ name: string; }'. // I tried <Person>{name: "Guy"} but it gives the same error. } 

How can I make the compiler ignore this problem until Im uses nonexistent properties or how to declare a class so that I can assign in this way instead of the new Person () and then set only the properties I want.

Note. Sending an object with all the properties to the constructor is not the solution I expect. Also using the interface works because I can declare the fields optional, how can I do the same in the class?

Adding additional information:

The main goal of what I'm trying to achieve is:

Find the best way to declare a class (not an interface) so that I can initialize it with clean code in one line, setting only the parameters that I want to set. In Java, you must overload constructors or use the Builder pattern.

I could try the creator pattern, but I'm looking for a typescript way to do this, if any.

+6
source share
4 answers

I found the easiest way to do this, and POSSIBLE. I think I tried this before, but the fact that I used the types "Number" and "String" instead of "number" and "string" could give me errors when skipping the compiler, the following code works wonders for me:

 export class Person{ constructor( public id?: number, // make sure you use number and string types instead of Number and String types public name?: string // make sure you make all properties optional using "?" ){} } export class PersonForm{ default:Person = <Person>{name: "Guy"}; } 
+3
source

You can write your class using the constructor.

 export class Person { constructor(public id: number, public name: string) {} } 

But you have to call it that.

 let person = new Person(null, "George"); 
0
source

Insert a JSON expression in any . The line for assigning default becomes:

 default: Person = <any>{ name: "Guy" }; 
0
source

This is another option that complements the other answers posted here.

If you have an object initializer

 { name:"John" } 

and you explicitly want an instance of the Person class, but you want to keep the object initializer, preserving the default values ​​that the Person class creates, you can do this by passing the entire object to the class constructor:

 var instance = new Person({name:"John"}); 

Where

 export class Person { id:number = 0; //or generate one name:string = "unknown"; constructor(opts:{id?:number; name?:string}){ if(opts){ this.id = opts.id || this.id; this.name = opts.name || this.name; } } } 

examples:

 var people = [ new Person(), new Person({name:"John"}), new Person({name:"James", id:10}), new Person({id:20})]; 
0
source

All Articles