-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
Bug Report
π Search Terms
Implicit type guard by other variables.
Related
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guards
β― Playground Link
π» Code
const state = {
prop1: 'value1',
prop2: 'value2',
prop3: 'value3',
}
type State = typeof state;
type Keys = (keyof State)
const makeSetProp = (path: Keys | undefined) => {
// "setProp" is not null if "path" is not undefined
const setProp =
path ?
(value: string) => state[path] = value :
null;
// "data" is not null is "path" is not undefined
const data =
path ?
'nextValue' :
null;
// "data" is always not null if "setProp" is not null, but the compiler does not recognize it
if (setProp) setProp(data); // Argument of type 'string | null' is not assignable to parameter of type 'string'
if (data) setProp(data); // Cannot invoke an object which is possibly 'null'.
if (path) setProp(data); // Cannot invoke an object which is possibly 'null'. Argument of type 'string | null' is not assignable to parameter of type 'string'.
if (setProp && data) setProp(data); // works
}π Actual behavior
datadoes not narrow by checking thesetPropsetPropdoes not narrow by checking thedatadataandsetPropboth do not narrow by checking thepatheven though they were defined by it
π Expected behavior
Logically they all can be narrowed by each other:
datacan be narrowed by checking thesetProporpathsetPropcan be narrowed by checking thedataorpath
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed