我在TypeScript中有一个界面。
interface Employee{
id: number;
name: string;
salary: number;
}
我想把'薪水'作为一个可以为空的领域(就像我们在C#中所做的那样)。这可以在TypeScript中完成吗?
答案 0 :(得分:206)
JavaScript(和TypeScript)中的所有字段都可以包含值null
或undefined
。
您可以将可选字段设为与可为空的
。interface Employee1 {
name: string;
salary: number;
}
var a: Employee1 = { name: 'Bob', salary: 40000 }; // OK
var b: Employee1 = { name: 'Bob' }; // Not OK, you must have 'salary'
var c: Employee1 = { name: 'Bob', salary: undefined }; // OK
var d: Employee1 = { name: null, salary: undefined }; // OK
// OK
class SomeEmployeeA implements Employee1 {
public name = 'Bob';
public salary = 40000;
}
// Not OK: Must have 'salary'
class SomeEmployeeB implements Employee1 {
public name: string;
}
与:比较:
interface Employee2 {
name: string;
salary?: number;
}
var a: Employee2 = { name: 'Bob', salary: 40000 }; // OK
var b: Employee2 = { name: 'Bob' }; // OK
var c: Employee2 = { name: 'Bob', salary: undefined }; // OK
var d: Employee2 = { name: null, salary: 'bob' }; // Not OK, salary must be a number
// OK, but doesn't make too much sense
class SomeEmployeeA implements Employee2 {
public name = 'Bob';
}
答案 1 :(得分:80)
在这种情况下,联盟类型是我心目中的最佳选择:
interface Employee{
id: number;
name: string;
salary: number | null;
}
// Both cases are valid
let employe1: Employee = { id: 1, name: 'John', salary: 100 };
let employe2: Employee = { id: 1, name: 'John', salary: null };
编辑:要使其按预期工作,您应在strictNullChecks
中启用tsconfig
。
答案 2 :(得分:27)
只需在可选字段中添加问号?
。
interface Employee{
id: number;
name: string;
salary?: number;
}
答案 3 :(得分:17)
要更像C#,请定义Nullable
类型,如下所示:
type Nullable<T> = T | null;
interface Employee{
id: number;
name: string;
salary: Nullable<number>;
}
答案 4 :(得分:6)
您可以仅实现如下所示的用户定义类型:
type Nullable<T> = T | undefined | null;
var foo: Nullable<number> = 10; // ok
var bar: Nullable<number> = true; // type 'true' is not assignable to type 'Nullable<number>'
var baz: Nullable<number> = null; // ok
var arr1: Nullable<Array<number>> = [1,2]; // ok
var obj: Nullable<Object> = {}; // ok
// Type 'number[]' is not assignable to type 'string[]'.
// Type 'number' is not assignable to type 'string'
var arr2: Nullable<Array<string>> = [1,2];
答案 5 :(得分:4)
我有一段时间也有同样的问题.. ts中的所有类型都可以为空,因为void是所有类型的子类型(例如,scala不同)。
查看此流程图是否有用 - https://github.com/bcherny/language-types-comparison#typescript
答案 6 :(得分:2)
type MyProps = {
workoutType: string | null;
};
答案 7 :(得分:0)
输入您的号码的值未定义
var user: Employee = { name: null, salary: undefined };
答案 8 :(得分:0)
可空类型可以调用运行时错误。
因此,我认为最好使用编译器选项--strictNullChecks
并将number | null
声明为类型。同样在嵌套函数的情况下,尽管输入类型为null,但是编译器不知道它会中断什么,因此我建议使用!
(感叹号)。
function broken(name: string | null): string {
function postfix(epithet: string) {
return name.charAt(0) + '. the ' + epithet; // error, 'name' is possibly null
}
name = name || "Bob";
return postfix("great");
}
function fixed(name: string | null): string {
function postfix(epithet: string) {
return name!.charAt(0) + '. the ' + epithet; // ok
}
name = name || "Bob";
return postfix("great");
}
参考。 https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions