在TypeScript类中,可以声明属性的类型,例如:
class className {
property: string;
};
如何在对象文字中声明属性的类型?
我已经尝试了以下代码,但无法编译:
var obj = {
property: string;
};
我收到以下错误:
名称“字符串”在当前范围中不存在
我是在做错什么还是错误?
在TypeScript类中,可以声明属性的类型,例如:
class className {
property: string;
};
如何在对象文字中声明属性的类型?
我已经尝试了以下代码,但无法编译:
var obj = {
property: string;
};
我收到以下错误:
名称“字符串”在当前范围中不存在
我是在做错什么还是错误?
Answers:
您已经很接近了,只需将替换=
为即可:
。您可以使用对象类型文字(请参阅规范第3.5.3节)或接口。使用对象类型文字与您所拥有的差不多:
var obj: { property: string; } = { property: "foo" };
但是您也可以使用界面
interface MyObjLayout {
property: string;
}
var obj: MyObjLayout = { property: "foo" };
经过多年的使用 const
并从更多的功能代码中受益,我建议大多数情况下不要使用以下代码。(在构建对象时,将类型系统强制为特定类型而不是让其推断类型通常表明存在问题)。
相反,我建议const
尽可能使用变量,然后将对象组成最后一步:
const id = GetId();
const hasStarted = true;
...
const hasFinished = false;
...
return {hasStarted, hasFinished, id};
如果您确实需要可以延迟初始化的类型:将其标记为可为空的联合类型(null或Type)。类型系统会阻止您在没有先确保其值的情况下使用它。
在中tsconfig.json
,确保启用严格的空检查:
"strictNullChecks": true
然后使用此模式,并允许类型系统保护您免受意外的空/未定义访问:
const state = {
instance: null as null | ApiService,
// OR
// instance: undefined as undefined | ApiService,
};
const useApi = () => {
// If I try to use it here, the type system requires a safe way to access it
// Simple lazy-initialization
const api = state?.instance ?? (state.instance = new ApiService());
api.fun();
// Also here are some ways to only access it if it has value:
// The 'right' way: Typescript 3.7 required
state.instance?.fun();
// Or the old way: If you are stuck before Typescript 3.7
state.instance && state.instance.fun();
// Or the long winded way because the above just feels weird
if (state.instance) { state.instance.fun(); }
// Or the I came from C and can't check for nulls like they are booleans way
if (state.instance != null) { state.instance.fun(); }
// Or the I came from C and can't check for nulls like they are booleans
// AND I was told to always use triple === in javascript even with null checks way
if (state.instance !== null && state.instance !== undefined) { state.instance.fun(); }
};
class ApiService {
fun() {
// Do something useful here
}
}
使用as
TSX运算符。
var obj = {
property: null as string
};
一个更长的例子:
var call = {
hasStarted: null as boolean,
hasFinished: null as boolean,
id: null as number,
};
使用强制转换运算符可以使其简洁(通过将null强制转换为所需的类型)。
var obj = {
property: <string> null
};
一个更长的例子:
var call = {
hasStarted: <boolean> null,
hasFinished: <boolean> null,
id: <number> null,
};
这比有两部分要好得多(一个用于声明类型,第二个用于声明默认值):
var callVerbose: {
hasStarted: boolean;
hasFinished: boolean;
id: number;
} = {
hasStarted: null,
hasFinished: null,
id: null,
};
property: null as string
其中重要的区别在于as
运算符。
call
第二个示例中分配了变量之后,是否可以为其分配完全不同的类型?
Error:(33, 15) TS2352:Type 'null' cannot be converted to type 'string'.
我很惊讶没有人提到这一点,但是您可以创建一个名为的接口ObjectLiteral
,该接口接受key: value
成对的type string: any
:
interface ObjectLiteral {
[key: string]: any;
}
然后,您将使用它,如下所示:
let data: ObjectLiteral = {
hello: "world",
goodbye: 1,
// ...
};
另外一个好处是,您可以根据需要在任意数量的对象上多次重复使用此接口。
祝好运。
[key: string]
部分,而不是将任何部分用作值的类型定义...那确实会消除类型的用途。
如果您尝试编写类型注释,则语法为:
var x: { property: string; } = ...;
如果您尝试编写对象文字,则语法为:
var x = { property: 'hello' };
您的代码试图在值位置使用类型名称。
var x: { property: string; } = ...;
是个逗人!我希望省略号是有效的简化语法var x: { property: string; } = { property: 'hello' };
。
如果您的属性具有相同的类型,则可以使用预定义的实用程序类型Record
:
type Keys = "property" | "property2"
var obj: Record<Keys, string> = {
property: "my first prop",
property2: "my second prop",
};
当然,您可以进一步定义属性值的自定义类型:
type Keys = "property" | "property2"
type Value = "my prop" | "my other allowed prop"
var obj: Record<Keys, Value> = {
property: "my prop",
property2: "my second prop", // TS Error: Type '"my second prop"' is not assignable to type 'Value'.
};
let
代替var
这里。
// Use ..
const Per = {
name: 'HAMZA',
age: 20,
coords: {
tele: '09',
lan: '190'
},
setAge(age: Number): void {
this.age = age;
},
getAge(): Number {
return age;
}
};
const { age, name }: { age: Number; name: String } = Per;
const {
coords: { tele, lan }
}: { coords: { tele: String; lan: String } } = Per;
console.log(Per.getAge());
在您的代码中:
var obj = {
myProp: string;
};
您实际上是在创建对象文字并将变量字符串分配给属性myProp。尽管这是非常糟糕的做法,但实际上这将是有效的TS代码(请勿使用此代码!):
var string = 'A string';
var obj = {
property: string
};
但是,您想要的是键入对象文字。这可以通过多种方式实现:
接口:
interface myObj {
property: string;
}
var obj: myObj = { property: "My string" };
自定义类型:
type myObjType = {
property: string
};
var obj: myObjType = { property: "My string" };
演员:
var obj = {
myString: <string> 'hi',
myNumber: <number> 132,
};
对象类型文字:
var obj: { property: string; } = { property: "Mystring" };