Hi all! I know TS pretty well (or so I thought) but am seeing some confusing behavior around type narrowing. Suppose I have these types:
type AAction = { type: 'a' }
type BAction = { type: 'b' }
type CAction = { type: 'c' }
type ARule = {
action: AAction
config: {
key1: 'key1'
}
}
type BRule = {
action: BAction
config: {
key1: 'key1'
}
}
type CRule = {
action: CAction
config: {
key2: 'key2'
}
}
type Rule = ARule | BRule | CRule
I can't tell why in the following code, TS is not able to see that r.config should only be { key1: 'key1' }:
let r: Rule
switch(r.action.type) {
case 'a':
r.config // this is { key1: 'key1' } | {key2: 'key2' }
}
By switching on r.action.type and selecting the 'a' case, why can TS not see that config must only be that of ARule?