1.TypeScript介绍
是由微软公司开发出来---------------------------C#, .NET, TypeScript 架构师(设计者) ,TypeScript 是开源的
TS 是 JS 的一个超集,静态类型,编译型语言。
tsc: compiler
Angular2------------- TS
Vue -------------- JS/TS
JS === ES6
2.数据类型
1.原始类型
undefined,null,number,string,boolean,symbol
示例:
function hello(name:string):string {
return "hello"+name;
}
console.log(hello("TS"));
// 静态类型
let a:number;
let b:boolean;
let c:string;
a=123;
b=1>2;
c=`a=${a},b=${b}`;
console.log(a);
console.log(b);
console.log(c);
2.对象类型
object
示例:
// 类型推断 number
// 数组存储相同类型的元素
let k:any[]=[1,2,3,4];
k.push("9");
k.push(true);
console.log(k);
// ------------------------------
// 泛型
let m:Array<any>;
m=[1,2,3,true,"8"];
console.log(m);
3.任意类型
any
示例:
// any 严格类型检查的同时,提供动态语言原来的灵活性
let x:any;
x=123;
console.log(`x=${x}`);
x="hello";
console.log(`x=${x}`);
x=true;
console.log(`x=${x}`);
any : 严格类型检查同时,提供动态语言原有的灵活
let x;===let x: any;
4.联合类型
let n: number | boolean;
n = 2;
n = true;
n = 'abc'; // error
5.函数类型
// 函数名
// 参数 *类型 、可选?
// 函数体
// 返回类型 *
/*
简单函数
@param x
*/
// 可选且存在默认值
function add(x:number,y:number=1000):number {
return x+y;
}
// ?可选
function add2(x:number,y?:number):number {
return x+y;
}
function bye(msg:string):void{
console.log(`再见 ${msg}`);
console.log(`欢迎下次光临`);
}
// console.log(add(2,3));
console.log(add(5));
bye('ES');
console.log(add2(2));
3.类型推断 / JS 变量提升
// 1 声明,赋值
let a; // any
a = 1;
a = 'abc'
// 2 声明初始化
let a = 1; // number
let x = 2 > 3;
let y = {};
let z = (x) => {return x + 1}
4.TypeScript和JavaScript以及ES6之间区别:
TypeScript 可以使用 JavaScript 中的所有代码和编码概念,TypeScript 是为了使 JavaScript 的开发变得更加容易而创建的。
TypeScript是Javascript的超集
TypeScript是ES6的超集
ES6是Javascript语言的标准,typescript是ES6的超集,Angular2是基于typescript来开发的JS框架