我需要帮助。
如何从代码中获取T接口名称:
interface User
{
id : number,
name : string
}
interface UserData
{
id : number,
user_id : number,
key : string,
value : string
}
function get<T>(){
const type = typeof(T);
}
get<User>();
get<UserData>();
get()方法的第一行返回错误:
const type = typeof(T);
如何得到T的类型?
谢谢。
首先,您需要一个T类型的参数作为输入。否则你就没有任何东西可以检查类型了。据我所知,没有typeof()函数。语法是"typeof 'What you want check'"→(这将以字符串形式返回)。您可以使用下面的代码。但这不会让你满意,因为它总是对象,因为typescript编译成javascript。
function get<T>(arg: T){
console.log(typeof arg);
} // will return object