断言与非空断言
#非空断言 #断言
断言与非空断言的笔记
断言
| const el = document.getElementById("sam") as HTMLImageElement;
el.src = "url地址";
|
正常将大类型转成它的子类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class PersonAs {}
class Student extends PersonAs { studying() { console.log("study..."); } }
function sayHello(p: PersonAs) { (p as Student).studying(); }
const stu = new Student(); sayHello(stu);
|
非空断言
1 2 3 4 5
| function printMsgLength(msg?: string) { console.log(msg!.length); }
|