อัปเดต 2019-05-15 (ปรับปรุงรูปแบบรหัสเป็นทางเลือก)
หลังจากใช้งานมาหลายปีconst
และได้รับประโยชน์จากโค้ดที่ใช้งานได้มากขึ้นผมขอแนะนำให้ใช้ต่อไปนี้ในกรณีส่วนใหญ่ (เมื่อสร้างวัตถุการบังคับให้ระบบประเภทเป็นประเภทเฉพาะแทนที่จะปล่อยให้มันอนุมานประเภทมักจะบ่งชี้ว่ามีบางอย่างผิดปกติ)
แต่ฉันขอแนะนำให้ใช้const
ตัวแปรให้มากที่สุดแล้วเขียนวัตถุเป็นขั้นตอนสุดท้าย:
const id = GetId();
const hasStarted = true;
...
const hasFinished = false;
...
return {hasStarted, hasFinished, id};
- สิ่งนี้จะพิมพ์ทุกอย่างอย่างเหมาะสมโดยไม่จำเป็นต้องพิมพ์อย่างชัดเจน
- ไม่จำเป็นต้องพิมพ์ชื่อฟิลด์อีกครั้ง
- สิ่งนี้นำไปสู่รหัสที่สะอาดที่สุดจากประสบการณ์ของฉัน
- สิ่งนี้ทำให้คอมไพเลอร์ให้การตรวจสอบสถานะมากขึ้น (ตัวอย่างเช่นหากคุณกลับมาในหลาย ๆ สถานที่ผู้รวบรวมจะให้แน่ใจว่าวัตถุชนิดเดียวกันนั้นจะถูกส่งคืนเสมอ - ซึ่งกระตุ้นให้คุณประกาศมูลค่าการส่งคืนทั้งหมดในแต่ละตำแหน่ง เจตนาของมูลค่านั้น)
เพิ่มเติม 2020-02-26
หากคุณต้องการชนิดที่คุณสามารถเริ่มต้นได้อย่างเกียจคร้าน: ทำเครื่องหมายว่าเป็นประเภทยูเนี่ยนที่สามารถยกเลิกได้ (โมฆะหรือประเภท) ระบบประเภทจะป้องกันคุณจากการใช้โดยไม่ต้องแน่ใจว่ามีค่า
ในtsconfig.json
ตรวจสอบให้แน่ใจว่าคุณเปิดใช้งานการตรวจสอบ null อย่างเข้มงวด:
"strictNullChecks": true
จากนั้นใช้รูปแบบนี้และอนุญาตให้ระบบประเภทปกป้องคุณจากการเข้าใช้ null / ไม่ได้กำหนดโดยไม่ตั้งใจ:
const state = {
instance: null as null | ApiService,
// OR
// instance: undefined as undefined | ApiService,
};
const useApi = () => {
// If I try to use it here, the type system requires a safe way to access it
// Simple lazy-initialization
const api = state?.instance ?? (state.instance = new ApiService());
api.fun();
// Also here are some ways to only access it if it has value:
// The 'right' way: Typescript 3.7 required
state.instance?.fun();
// Or the old way: If you are stuck before Typescript 3.7
state.instance && state.instance.fun();
// Or the long winded way because the above just feels weird
if (state.instance) { state.instance.fun(); }
// Or the I came from C and can't check for nulls like they are booleans way
if (state.instance != null) { state.instance.fun(); }
// Or the I came from C and can't check for nulls like they are booleans
// AND I was told to always use triple === in javascript even with null checks way
if (state.instance !== null && state.instance !== undefined) { state.instance.fun(); }
};
class ApiService {
fun() {
// Do something useful here
}
}
อย่าทำด้านล่างใน 99% ของกรณี:
อัพเดท 2016-02-10 - จัดการ TSX (ขอบคุณ @Josh)
ใช้as
โอเปอเรเตอร์สำหรับ TSX
var obj = {
property: null as string
};
ตัวอย่างที่ยาวกว่า:
var call = {
hasStarted: null as boolean,
hasFinished: null as boolean,
id: null as number,
};
คำตอบเดิม
ใช้ตัวดำเนินการร่ายเพื่อทำให้รวบรัดนี้ (โดยการร่ายโมฆะเป็นประเภทที่ต้องการ)
var obj = {
property: <string> null
};
ตัวอย่างที่ยาวกว่า:
var call = {
hasStarted: <boolean> null,
hasFinished: <boolean> null,
id: <number> null,
};
สิ่งนี้ดีกว่าการมีสองส่วน (หนึ่งเพื่อประกาศประเภทที่สองเพื่อประกาศค่าเริ่มต้น):
var callVerbose: {
hasStarted: boolean;
hasFinished: boolean;
id: number;
} = {
hasStarted: null,
hasFinished: null,
id: null,
};