- 原理
他是javascript的一个超集,在原有语法基础上,添加可选的静态类型和基于类的面向对象编程
- 和js的区别
-- 面向项目:
ts: 面向解决大型复杂项目,架构以及代码维护复杂场景
js: 脚本化语言,用于面向简单场景
-- 自助检查:
ts: 编译期间,主动发现并纠正错误
js: 运行时报错
-- 类型检测:
ts: 弱类型,支持对动态和静态数据类型的检测
js: 弱类型,无静态类型
-- 运行流程
ts: 依赖编译,依赖工程化体系
js: 直接在浏览器中运行
-- 复杂程度
ts: 模块化,泛型,接口
- 安装
npm i -g typescript
- 编译
tsc test.ts
所有的类型检测和语法检测都是在编译时报错
- ts基础类型豫写法
boolean,string,number,array,null,undefined
let isEnabled = true;
let class = "xiaoyuan"
let classNumber = 2
let u = undefined
let n = null
let classArr = ['base','execute']
let isEnabled:boolean = true;
let class:string = "xiaoyuan"
let classNumber:number = 2
let u:undefined = undefined
let n:null = null
let classArr:string[] = ['base','execute']
let classArr:Array<string> = ['base','execute']
let tupleType = [string,boolean]
tupleType = ['xiaoyuan',true]
enum Score{
BAD,
NG,
GOOD,
PERFECT
}
let sco: Score = Score.BAD;
enum Score{
BAD='bad',
NG="ng",
GOOD="good",
PERFECT="perfect"
}
let scoName = Score[0]
let scoVal = Score['bad']
enum Enum{
A,
B,
C="C",
D="D",
E=6,
F
}
let anyVal = 1
let anyVal = "xiaoyuan"
let anyVal = true
let numberVal:number = anyVal
let unkonwnVal:unknown;
unkonwnVal= 1
unkonwnVal= "xiaoyuan"
unkonwnVal= true
let val:unknown = unkonwnVal;
let val:any = unkonwnVal;
let val:boolean = unkonwnVal;
function voidFn():void{
}
function error(msg:string):never{
}
interface objConstructor{
create(o:object|null):any
}
interface object{
constructor:Function;
toString():string;
toLocaleString():string;
valueOf(): Object;
hasOwnProperty(v:PropertyKey):boolean;
isPropertypeOf(v:Object):boolean;
}
const obj= {}
obj.prop = "xiaoyuan";
obj.valueOf()
interface Class{
name:string;
tiem:number;
}
let classRoom:Class = {
name:"ts",
time:2
}
interface Class{
readonly name:string;
time:number
}
let arr:number[] = [1,2,3,4,5]
let ro: ReadonlyArray<number> = arr;
ro[0] = 12
ro.push(5)
ro.length = 10
arr = ro;
interface Class{
readonly name:string;
time:number;
[propName:string]:any;
}
const c1 = {name:"js",time:1}
const c2 = {name:"js",time:1,level:1}
interface A{x:D}
interface B{x:E}
interface C{x:F}
interface D{d:number}
interface E{e:string}
interface F{f:boolean}
type ABC = A & B & C;
let abc:ABC = {
x:{
d:24,
e:"xiaoyuan",
f: true
}
}
interface A{
c:string;
d:string;
}
interface B{
c:number;
e:string;
}
type AB = A & B
let ab:AB = {
d:'xiaoyuan',
e:'xiaoyuan'
}
let anyVal:any = "hi zhaowa"
let anyLength:number = (<string>anyVal).length;
let anyLength:number = (anyVal as string).length;
type ClassTime = () => number;
const start = (classTime:ClassTime | undefined)=>{
let num = classTime!();
}
let tsClass:number | undefined = undefined;
const age:number = tsClass!;
interface Teacher{
name:string;
courses:string[];
}
interface Student{
name:string;
startTime:Date;
}
type Cls = Teacher | Student
function Course(cls:Cls){
if('courses' in cls){
}
}
typeof cls.score == "number"
cls instanceof Teacher
const isTeacher = function(cls:Teacher|Student):cls is Teacher{
}
const getName = (cls:Teacher | Student)=>{
if(isTeacher(cls)){
}
}
class Cls{
start(name:number;score:number):number;
start(name:string;score:string):string;
start(name:string;score:number):number;
start(name:Comnbinable;score:Comnbinable){
}
}
function startClass<T,U>(name:T,score:U):T{}
function startClass<T,U>(name:T,score:U):T{
return (name+String(score)) as any as T;
}
function fn(target:Function):void{
target.prototype.startClass = function():void{}
}
@fn
class Course{
}
var a = 2;
[
'var':'keyword',
'a':'identifier',
'=':'assignment',
'2':'imteger',
';':'eos'
]
{
operation: '=',
left:{
keyword:'var',
right:'a'
},
right:'2'
}