主页
TypeScript
TypeScript 特殊类型
demo_special_types_with_unknown
运行 ❯
×
更改方向
保存代码
更改主题
let w: unknown = 1; w = "string"; // no error w = { runANonExistentMethod: () => { console.log("I think therefore I am"); } } as { runANonExistentMethod: () => void } // How can we avoid the error for the code commented out below when we don't know the type? // w.runANonExistentMethod(); // Error: Object is of type 'unknown'. if(typeof w === 'object' && w !== null) { (w as { runANonExistentMethod: Function }).runANonExistentMethod(); } // Although we have to cast multiple times we can do a check in the if to secure our type and have a safer casting
I think therefore I am