/*
* 布尔值 boolean
*/
let isDone: boolean = false;
// 因为Boolean返回的是对象,所以无法使用普通数据类型boolean
// let createdByNewBoolean: boolean = new Boolean(1);
/*
* 字符串 string
*/
let myName: string = ‘Tom‘;
let myAge: number = 25;
let sentence: string = `Hello, my name is ${myName}.
I‘ll be ${myAge + 1} years old next month.`;
/*
* 函数 function
*/
function alertName(): void {
alert(‘My name is Tom‘);
}
/*
* 接口 interface
*/
// ts中一旦定义了任意属性,那么确定属性和可选属性的类型都必须是它的类型的子集(特别注意)
// 有时候我们希望对象中的一些字段只能在创建的时候被赋值,那么可以用 readonly 定义只读属性
// 注意,只读的约束存在于第一次给对象赋值的时候,而不是第一次给只读属性赋值的时候
interface Person {
readonly id: number;
name: string;
age?: number;
[propName: string]: any;
}
interface NumberArray {
[index: number]: string;
}
let fibonacci: NumberArray = ["1", "1", "2", "3", "5"];
原文:https://www.cnblogs.com/universe-cosmo/p/11209009.html