Skip to content

Instantly share code, notes, and snippets.

@abigmiu
Created December 23, 2022 11:49
Show Gist options
  • Save abigmiu/19691e181384864c13c8e9ccd669a6c0 to your computer and use it in GitHub Desktop.
Save abigmiu/19691e181384864c13c8e9ccd669a6c0 to your computer and use it in GitHub Desktop.
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