Поскольку javascript не имеет type checking
, то вы можете сделать someVar="hello"
, а затем вы можете присвоить значение другого типа, e .г. boolean, как someVar=true
, что хорошо в javascript. Тип сценарий предлагает возможности проверки типов среди других функций для javascript. Это не имеет ничего общего с инициализацией.
=
устанавливает значение переменной
:
устанавливает тип переменной
В вашем примере:
public heroes = HEROES; // assigns value of HEROES to heroes, heroes now has an inferred type same as the type of HEROES
title = "Tour of Heroes"; // normal assignment with inferred type of 'string'
selectedHero: Hero; // just declares selectedHero with the type 'Hero'
Вы можете установить значение и тип в то же время:
title:string = "some text"; // this means, title is of type string and has the value "some text"
позже, если вы это сделаете, title=true
компилятор даст вам предупреждение, потому что вы пытаетесь присвоить логическое значение переменной с типом.
Extra Вы также можете установить на несколько типов, а не только один:
title:string|boolean=true; // title is of type either string or boolean
title:"text1"|"text2"|"text3"; // (v1.8 and after) title is of type string and can have only one of the values: "text1","text2" or "text3". in other words enum type
title:any; // title is of any type.
На объявлении функции:
someFunction(name:string):boolean{
// the parameter 'name' is expected to be of type string in the body of this function
return true; // the return type of the function is expected to be boolean
}
лямбда-выражения:
someFunction = (name:string):boolean => {
// the variable name is expected to be of type string in the body of this function
return true;
}
Дальнейшее чтение:
Typescript specs for Types
Typescript Type System
Угловой - это просто Javascript, поэтому я ожидаю, что вы нашли ':' в объявлении объекта –
Я вижу, что я думал, что это должно быть так: - var: = first_name: "John", lastName: "Doe", возраст: 50, eyeColor: "blue"}; ', но, похоже, это не нужно, спасибо в любом случае. –