0

I want to specify an interface to my object like this:

interface Item {
    id: number;
    size: number;
}


var obj  = {
    name: "test",
    items:Item = null 
}

and I want to assign obj.items some value after I fetched some data from server on ngOninit().

is that possible?

1
  • var obj: { name: string, items: Item | null } = { ... }? Or you could use items?: Item and default to undefined, either implicitly or explicitly. Commented Jun 24, 2019 at 11:20

2 Answers 2

2

Construction of Type

interface Item {
    id: number;
    size: number;
}

interface ParentItem {
    name: string;
    item: Item | null;
}

Construction of Object:

let object: ParentItem = { name: "test", item: null };
Sign up to request clarification or add additional context in comments.

3 Comments

Despite the acceptance note that the construction example is neither: 1. valid syntax; nor 2. type safe.
@jonrsharpe updated the syntax, I am not sure to understand about type safety.
You say it is always an Item then set it to null. Either make it nullable or optional.
0
export interface ClientEmployee {
clientTeamID: number;
employeeID: number;
sharingPercent: number;
}
import 
 ClientEmployees = {
id: 0,
clientTeamID: 0,
employeeID: 0,
sharingPercent: 0,
clientName: ''
  };
 get in oninit 

 this.restService.GetTeamEmployees('').subscribe(response => {
  if (response.error == null && response.message === 'Success' && 
 response.data.length > 0) {
    this.ClientEmployees = response.data;
    this.loaderService.hide();
    }  });

1 Comment

What does this even mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.