在 TypeScript 中,可以使用 console.log 函数来打印变量的类型。以下是一些示例:

let num: number = 5;
console.log(typeof num);  // 输出 "number"

let str: string = "Hello";
console.log(typeof str);  // 输出 "string"

let bool: boolean = true;
console.log(typeof bool);  // 输出 "boolean"

let arr: number[] = [1, 2, 3];
console.log(Array.isArray(arr));  // 输出 "true"

let obj: { name: string, age: number } = { name: "John", age: 25 };
console.log(obj instanceof Object);  // 输出 "true"

上述示例中,typeof 操作符可以打印基本类型的类型,Array.isArray 函数可以打印数组类型,instanceof 操作符可以打印对象类型。

TypeScript 中如何打印变量类型 - 详细示例讲解

原文地址: https://www.cveoy.top/t/topic/pSWI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录