typescript automatically inferring type of a union indirectly

Here is the example which is self-explementary:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type TypeX = {t:"a", a1:string} | {t:"b", a2: string};
let t: TypeX | undefined;
function setA(){
t = {t:"a", a1:"values"};
}
setA();
console.log(t.a1);
</code>
<code>type TypeX = {t:"a", a1:string} | {t:"b", a2: string}; let t: TypeX | undefined; function setA(){ t = {t:"a", a1:"values"}; } setA(); console.log(t.a1); </code>
type TypeX = {t:"a", a1:string} | {t:"b", a2: string};
let t: TypeX | undefined;
function setA(){
    t = {t:"a", a1:"values"};
}
setA();
console.log(t.a1);

TS complians on last line:

Property ‘a1’ does not exist on type ‘TypeX’.
Property ‘a1’ does not exist on type ‘{ t: “b”; a2: string; }’.(2339)

Other than casting t to {t:"a", a1:string}, is there ay other way to tell TS, hey I know the type is {t:"a", a1:string} because setA is just called.

I am looking for something like, “hey TS, when funcation setA is called, the t type is {t:"a", a1:string}“.

BTW, I know I can do runtime check like the following, but I am looking for more a compile time check and a bit smarter than an additional if:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if(t && t.t === "a"){
console.log(t.a1);
}
</code>
<code>if(t && t.t === "a"){ console.log(t.a1); } </code>
if(t && t.t === "a"){
    console.log(t.a1);
}

TS does some control-flow analysis, but does not inline function calls.

When seeing a function call, it has to decide what effect it has on local variables. Currently, it does not even attempt any analysis, and assumes that there are no side effects.

See Trade-offs in Control Flow Analysis #9998

The primary question is: When a function is invoked, what should we assume its side effects are?

One option is to be pessimistic and reset all narrowings, assuming that any function might mutate any object it could possibly get its hands on. Another option is to be optimistic and assume the function doesn’t modify any state. Both of these seem to be bad.

This problem spans both locals (which might be subject to some “closed over or not” analysis) and object fields.

Current approach is optimistic, which results in missing narrowing in your example, and runtime error in example below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type TypeX = {t:"a", a1:string} | {t:"b", a2: string};
let t: TypeX | undefined = {t:"a", a1:"values"};
function setA(){
t = undefined;
}
setA();
console.log(t.a1);
</code>
<code>type TypeX = {t:"a", a1:string} | {t:"b", a2: string}; let t: TypeX | undefined = {t:"a", a1:"values"}; function setA(){ t = undefined; } setA(); console.log(t.a1); </code>
type TypeX = {t:"a", a1:string} | {t:"b", a2: string};
let t: TypeX | undefined = {t:"a", a1:"values"};

function setA(){
  t = undefined;
}

setA();
console.log(t.a1);

TS cannot realistically inline all function calls and analyze resulting paths – and currently it does not even try. Function calls can explode in complexity – functions can call other functions, call themselves recursively etc. In general you cannot even prove that a function will terminate.

Currently there are no mechanisms to annotate a function’s effect on closed-over variables. The closest “annotate side effect” feature is called an assertion function, but that only tracks side effects of things passed to the function, not closed-over variables.

There are some proposals to enable flow control analysis for immediately-invoked functions like Annotate immediately-invoked functions for inlining flow control analysis #11498

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật