Answers:
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';
}
"strict" : false
salary:number|null;
如果您这样做,salary?:number; salary = null;
则会出现错误。但是,salary = undefined;
在这种情况下可以正常工作。解决方案:使用联合,即“ |”
在这种情况下,我认为联合类型是最佳选择:
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
in tsconfig
。
要更像C#,请定义以下Nullable
类型:
type Nullable<T> = T | null;
interface Employee{
id: number;
name: string;
salary: Nullable<number>;
}
奖金:
要使Nullable
行为类似于内置的Typescript类型,请global.d.ts
在根源文件夹的定义文件中对其进行定义。这条路对我有用:/src/global.d.ts
emp: Partial<Employee>
,我们可以做某事emp.id
,emp.name
等等,但是如果我们拥有emp: Nullable<Employee>
,我们就不能做emp.id
只需?
在可选字段中添加一个问号即可。
interface Employee{
id: number;
name: string;
salary?: number;
}
您可以像下面这样实现用户定义的类型:
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];
不久前我也遇到了同样的问题。ts中的所有类型都是可为空的,因为void是所有类型的子类型(例如,与scala不同)。
查看此流程图是否有帮助-https: //github.com/bcherny/language-types-comparison#typescript
void
“所有类型的子类型”(底部类型),请参考此线程。同样,您为scala提供的图表也不正确。Nothing
实际上,在scala中,bottom是底部类型。打字稿atm 没有底部字体,而scala 却没有。
可空类型可以调用运行时错误。因此,我认为最好使用编译器选项--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
typescript@next
现在。)