Câu trả lời:
Tất cả các trường trong JavaScript (và trong TypeScript) có thể có giá trị null
hoặc undefined
.
Bạn có thể làm cho trường tùy chọn khác với nullable.
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;
}
So sánh với:
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;
Nếu bạn làm salary?:number; salary = null;
Bạn sẽ gặp lỗi. Tuy nhiên, salary = undefined;
sẽ hoạt động tốt trong trường hợp này. Giải pháp: sử dụng Union tức là '|'
Loại liên minh là trong tâm trí của tôi lựa chọn tốt nhất trong trường hợp này:
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 };
EDIT: Đối với điều này để làm việc như mong đợi, bạn nên kích hoạt strictNullChecks
trong tsconfig
.
Để giống C # hơn , hãy xác định Nullable
loại như thế này:
type Nullable<T> = T | null;
interface Employee{
id: number;
name: string;
salary: Nullable<number>;
}
Tặng kem:
Để thực hiện Nullable
hành vi giống như một kiểu Bản mô tả, hãy xác định nó trong global.d.ts
tệp định nghĩa trong thư mục nguồn gốc. Con đường này làm việc cho tôi:/src/global.d.ts
emp: Partial<Employee>
, chúng ta có thể làm emp.id
hoặc emp.name
vân vân nhưng nếu có emp: Nullable<Employee>
, chúng ta không thể làm đượcemp.id
Chỉ cần thêm một dấu hỏi ?
vào trường tùy chọn.
interface Employee{
id: number;
name: string;
salary?: number;
}
Bạn chỉ có thể thực hiện một loại do người dùng xác định như sau:
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];
tôi đã có cùng một câu hỏi một lúc trước .. tất cả các loại trong ts là nullable, bởi vì void là một kiểu con của tất cả các loại (không giống như, ví dụ, scala).
xem liệu lưu đồ này có giúp được không - https://github.com/bcherny/lingu-types-comparison#typescript
void
'loại phụ của tất cả các loại' ( loại dưới cùng ), hãy tham khảo chủ đề này . Ngoài ra biểu đồ bạn cung cấp cho scala là không chính xác. Nothing
trong scala, trên thực tế, là loại dưới cùng. Bản đánh máy, atm, không có loại dưới cùng trong khi scala thì có .
Loại Nullable có thể gọi lỗi thời gian chạy. Vì vậy, tôi nghĩ rằng tốt để sử dụng một tùy chọn trình biên dịch --strictNullChecks
và khai báo number | null
là loại. cũng trong trường hợp hàm lồng nhau, mặc dù kiểu đầu vào là null, trình biên dịch không thể biết nó có thể phá vỡ cái gì, vì vậy tôi khuyên bạn nên sử dụng !
(dấu chấm than).
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");
}
Tài liệu tham khảo. https://www.typescriptlang.org/docs/handbook/advified-types.html#type-guards-and-type-assertions
typescript@next
bây giờ.)