Answer the question
In order to leave comments, you need to log in
What is alterEgo?
In the tutorial about heroes there is this
constructor(
public id: number,
public name: string,
public power: string,
public alterEgo?: string
){/*body*/}
Answer the question
In order to leave comments, you need to log in
Optional parameter
UPD:
When you declare a public parameter in a constructor, this property is automatically declared on the class, but ? refers to a parameter, not a property. So you can do this
class Hero {
...
public alterEgo?: string;
constructor(..., _alterEgo?: string) {
this.alterEgo = _alterEgo;
}
}
Alexander Marchenko Then why if so:
heroes = [
new Hero(1, 'Windstorm', 'Really Smart', 'optional'),
new Hero(13, 'Bombasto', 'Super Bad Man' ),
];
hero: Hero = {
id: 1,
name: 'Windstorm',
power: 'Black T'
};
app/app.component.ts(32,3): error TS2322: Type '{ id: number; name: string; power: string; }' is not assignable to type 'Hero'.
Property 'alterEgo' is missing in type '{ id: number; name: string; power: string; }'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question