在类中的TypeScript中,可以声明属性的类型,例如:
class className {
property: string;
};
Run Code Online (Sandbox Code Playgroud)
我应该如何编写代码来声明对象文字中属性的类型?这样的代码不编译:
var obj = {
property: string;
};
Run Code Online (Sandbox Code Playgroud)
(我收到错误 - 当前范围中不存在名称'string').
我做错了什么或者是一个错误?
我有这样的情况:
interface MoverShaker {
getStatus(): { speed: number; frequency: number; };
}
function GetMoverShaker() : MoverShaker {
return {
getStatus: () => { speed: 2, frequency: 3 }
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:当前范围中不存在名称"频率".TypeScript中是否可以构建此类构造?如果我使用这样的结构,那么一切都还可以:
function GetMoverShaker(): MoverShaker {
return {
getStatus: () => {
return { speed: 2, frequency: 3 }
}
}
Run Code Online (Sandbox Code Playgroud) typescript ×2