Closed
Description
π Search Terms
null undefined narrow narrowing reduction equality narrowby
π Version & Regression Information
- This is the behavior in every version I tried*
*it worked differently in 5.1-5.3 because those had a bug in them ( #57202 )
β― Playground Link
π» Code
type AnyObject = Record<string, any>;
type State = AnyObject;
declare function hasOwnProperty<T extends AnyObject>(
object: T,
prop: PropertyKey,
): prop is keyof T;
interface Store<S = State> {
setState<K extends keyof S>(key: K, value: S[K]): void;
}
export function syncStoreProp<
S extends State,
P extends Partial<S>,
K extends keyof S,
>(store: Store<S>, props: P, key: K) {
const value = hasOwnProperty(props, key) ? props[key] : undefined;
if (value === undefined) return;
// this errors
store.setState(key, value);
// this shouldn't affect the type anyhow, we've already checked against the same thing
if (value === undefined) return;
// suddently it succeeds
store.setState(key, value);
}
π Actual behavior
one of the calls errorrs while the other one doesn't
π Expected behavior
i'd expect consistent behavior at both call sites
Additional information about the issue
originally reported by @diegohaz here: https://twitter.com/diegohaz/status/1765674095293747628