Ami*_*abh 202 javascript typescript
我在TypeScript中有一个接口.
interface Employee{
id: number;
name: string;
salary: number;
}
Run Code Online (Sandbox Code Playgroud)
我想把'薪水'作为一个可以为空的领域(就像我们在C#中所做的那样).这可以在TypeScript中做到吗?
Rya*_*ugh 238
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;
}
Run Code Online (Sandbox Code Playgroud)
与之比较:
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';
}
Run Code Online (Sandbox Code Playgroud)
bja*_*sic 104
在这种情况下,联盟类型是我心目中的最佳选择:
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 };
Run Code Online (Sandbox Code Playgroud)
编辑:为了使其按预期工作,您应该启用strictNullChecksin tsconfig.
Tim*_*ord 45
要更像C#,请定义如下Nullable类型:
type Nullable<T> = T | null;
interface Employee{
id: number;
name: string;
salary: Nullable<number>;
}
Run Code Online (Sandbox Code Playgroud)
Mig*_*ura 30
只需?在可选字段中添加问号即可.
interface Employee{
id: number;
name: string;
salary?: number;
}
Run Code Online (Sandbox Code Playgroud)
Yil*_*maz 19
type Nullable<T> = {
[P in keyof T]: T[P] | null;
};
Run Code Online (Sandbox Code Playgroud)
然后你就可以使用它了
Nullable<Employee>
Run Code Online (Sandbox Code Playgroud)
这样你仍然可以使用Employee接口,因为它在其他地方
Rit*_*wik 10
type MyProps = {
workoutType: string | null;
};
Run Code Online (Sandbox Code Playgroud)
小智 8
可空类型可能会引发运行时错误。所以我认为最好使用编译器选项--strictNullChecks并声明number | 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");
}
Run Code Online (Sandbox Code Playgroud)
参考。 https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions
type WithNullableFields<T, Fields> = {
[K in keyof T]: K extends Fields
? T[K] | null | undefined
: T[K]
}
let employeeWithNullableSalary: WithNullableFields<Employee, "salary"> = {
id: 1,
name: "John",
salary: null
}
Run Code Online (Sandbox Code Playgroud)
或者您可以关闭 strictNullChecks ;)
以及相反的版本:
type WithNonNullableFields<T, Fields> = {
[K in keyof T]: K extends Fields
? NonNullable<T[K]>
: T[K]
}
Run Code Online (Sandbox Code Playgroud)
您可以像下面这样实现用户定义的类型:
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];
Run Code Online (Sandbox Code Playgroud)