ฉันมีส่วนต่อประสานใน TypeScript
interface Employee{
id: number;
name: string;
salary: number;
}
ฉันต้องการที่จะทำให้salaryเป็นเขตข้อมูลโมฆะ (เช่นที่เราสามารถทำได้ใน C #) เป็นไปได้ที่จะทำใน TypeScript?
ฉันมีส่วนต่อประสานใน TypeScript
interface Employee{
id: number;
name: string;
salary: number;
}
ฉันต้องการที่จะทำให้salaryเป็นเขตข้อมูลโมฆะ (เช่นที่เราสามารถทำได้ใน C #) เป็นไปได้ที่จะทำใน TypeScript?
คำตอบ:
ทุกสาขาใน JavaScript (และใน typescript) สามารถมีค่าหรือnullundefined
คุณสามารถทำให้ฟิลด์เป็นตัวเลือกซึ่งแตกต่างจาก 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;
}
เปรียบเทียบกับ:
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;จะใช้งานได้ดีในกรณีนี้ วิธีแก้ปัญหา: ใช้ Union ie '|'
ประเภทสหภาพอยู่ในใจตัวเลือกที่ดีที่สุดในกรณีนี้:
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 };
แก้ไข: สำหรับการทำงานตามที่คาดไว้คุณควรเปิดใช้งานในstrictNullCheckstsconfig
หากต้องการเป็น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];
type MyProps = {
workoutType: string | null;
};
ฉันมีคำถามเดียวกันนี้ในขณะที่กลับ .. ทุกประเภทใน ts เป็นโมฆะเพราะเป็นโมฆะเป็นประเภทย่อยของทุกประเภท (ซึ่งแตกต่างจากตัวอย่างเช่นสกาล่า)
ดูว่าผังงานนี้ช่วยได้หรือไม่ - https://github.com/bcherny/language-types-comparison#typescript
voidเป็น 'ประเภทย่อยของทุกประเภท' ( ประเภทด้านล่าง ) อ้างถึงหัวข้อนี้ แผนภูมิที่คุณระบุสำหรับสกาลานั้นไม่ถูกต้องเช่นกัน Nothingในสกาล่าคือในความเป็นจริงประเภทด้านล่าง typescript, ATM, ไม่ได้มีประเภทด้านล่างขณะที่สกาล่าไม่
ประเภทที่ใช้งานไม่ได้สามารถเรียกใช้ข้อผิดพลาดขณะทำงาน ดังนั้นฉันคิดว่าเป็นการดีที่จะใช้ตัวเลือกคอมไพเลอร์--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");
}
การอ้างอิง https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions
typescript@nextตอนนี้)