// 尖括号定义类型let value:any="this is a string"let valueLen:number=<string>Value.length
// as 强制转换类型let str:any="this is a string"let strLen:number=(str asstring).length
// 非空断言functionmyFunc(mayBeString:string| undefined |null){const onlyString:string= mayBeString
// Initializer type string | undefined | null is not assignable to variable type string const ignoreUndefinedAndNull:string= mayBeString!// true}typeNumGenerator=()=>numberfunctionmyFunc(numGenerator: NumGenerator | undefined){const num1 =numGenerator()// Method expression can be null or undefinedconst num2 = numGenerator!()// 函数断言忽略undefined}// 确定赋值断言let x:numberinitialize()console.log(2* x)// Variable 'x' is used before being assigned.functioninitialize(){
x =10}let x!:numberinitialize()console.log(2* x)// 20functioninitialize(){
x =10}
类型断言// 尖括号定义类型let value: any = "this is a string"let valueLen: number = <string>Value.length// as 强制转换类型let str: any = "this is a string"let strLen: number = (str as string).length// 非空断言function myFunc(mayBeString: string | undefined | nul