Created
December 23, 2022 11:49
-
-
Save abigmiu/19691e181384864c13c8e9ccd669a6c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isString(test: any): test is string{ | |
return typeof test === "string"; | |
} | |
function example(foo: any){ | |
if(isString(foo)){ | |
console.log("it is a string" + foo); | |
console.log(foo.length); // string function | |
// 如下代码编译时会出错,运行时也会出错,因为 foo 是 string 不存在toExponential方法 | |
console.log(foo.toExponential(2)); | |
} | |
// 编译不会出错,但是运行时出错 | |
console.log(foo.toExponential(2)); | |
} | |
example("hello world"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment