TypeScript : Colon vs Equal To ( Angular Tutorial )

后端 未结 6 1813
Happy的楠姐
Happy的楠姐 2020-12-06 10:35

I am learning Angular4 and going through the tutorial examples.

https://angular.io/docs/ts/latest/tutorial/toh-pt1.html

I have the below code in the tutoria

6条回答
  •  天命终不由人
    2020-12-06 11:08

    I would say it is kind of interface but instead of implementing it you can use this class as a type:

    export class Hero {
      id: number; // *2
      name: string;
    }
    
    let a: Hero = 'some call to create new hero';
    

    It will ensure that the object is a type anything else will be cause in type error.

    Here you can see it is been used as a type to create a new Hero of type Hero:

    hero: Hero = {
      id: 1,
      name: 'Windstorm'
    };
    

提交回复
热议问题