Control Flow Analysis
Definition:
At each instruction, a variable's type may be narrowed according to the current context.
Some expressions (
typeof x === 'string'
) act as "type guards", narrowing the possible types of a variable inside a context (the if statement);x
is narrowed fromnumber|string
tostring
inside the if block;x
only can bynumber
at the last line, since theif
block returns;The function gets an inferred return type corresponding to an union of all return paths;
Discriminated union
The type
Actions
below is called a discriminated union . The propertytype
is used as a tag to filter out which of the union options is valid at the context;At each
case
line below,action.data
has its type narrowed down;
Last updated