กฎการกำหนดขอบเขตสำหรับการตรวจสอบชนิดที่ไม่กำหนดใน Typescript
ฉันกำลังเล่นกับโค้ดที่เป็นปัญหา Unexpected "never" ใน Typescript
ฉันกำลังพยายามเข้าใจการตรวจสอบค่า undefined และสมมติว่าตัวแปรถูกตีความว่าจะไม่เป็น null/undefined ในสโคปของบล็อกทั้งหมดที่ตามหลังการตรวจสอบ
interface IProperties {
foo?: {
aaa: string
bbb: string
}
}
function init(properties: IProperties) {
if (properties.foo) {
type FooOK = typeof properties.foo;
for (const x of [1, 2, 3]) {
type FooOrUndefined = typeof properties.foo;
}
}
}
ประเภทถูกสรุปดังนี้:
type FooOK = { aaa: string; bbb: string;}
type FooOrUndefined = { aaa: string; bbb: string;} | undefined
มีใครสามารถอธิบายได้ไหมว่าทำไมชนิดในลูปถึงไม่พิจารณาตัว guard
ลิงก์สนามเด็กเล่น