Skip to content

null gets accidentally eliminated when narrowing by undefined's equalityΒ #57693

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

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240308#code/C4TwDgpgBAggdiA8gIwFYQMbCgXigJUwHsAnAEwB4BnYEgSzgHMAaKAQwQD4BuAKFEhQAysDbBoeeEjSZgfXmUwAbNiWgAzAK5wsdInCgALNlUQB3OAAUSRSCVAUAKlAgAPcXDJVYCFOiycABS8UFBEMlgAXFCOzCFQYDZg0da2EPYgANIQIHEAlNGJtlB03gDWOUTqMfIM4iTqbBjQIqQQFEK4wqLinFAA3vFUEMAiYu2ZLu4QnuWV1UJBFSDRmawAbmxKmhDRQgDamQC6BVDrRHRkfAC+vLxuYKTYWjrAegZUIDqtaqlgFPFOm4PF5uuM4qFLFMQd5LKo3lsOpwIVBJsCZqDllVhHEgjQ2ntgG0kawimAqClWMtVnkBvEMPoaGctjsusZTBY-ulQIEyVQqTlaQB+BJJKj7ZZHKDRbSKdQMCBXO6hOjVQKbbYSHB4WUQeVwRW0tTATQkOB8UIAektUGAhlKLhINhIVCGRLUADphqMehBAssNiyIHl5FabXaHVRDERNEoyHAAOTYNjqdSyW2GaACaAcEDRsysMwQBPrHNKNRsMggKAYTMYCpkdiMNgMJl26BUNgAWyz9qY8VVUHVQdw2qguv1hqgxtN5vi1qgVE0ZEUcGASmrdGwS4wzUVrtC+M93rG4n9OUDmpDvFuQA

πŸ’» 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 Haz (@diegohaz) here: https://twitter.com/diegohaz/status/1765674095293747628

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions