Skip to content

Instantly share code, notes, and snippets.

@syusui-s
Created May 24, 2020 15:16
Show Gist options
  • Select an option

  • Save syusui-s/a1874a4a91e502f1a6b602ad455cacde to your computer and use it in GitHub Desktop.

Select an option

Save syusui-s/a1874a4a91e502f1a6b602ad455cacde to your computer and use it in GitHub Desktop.
class S {
sProps: string = "sProps"
}
class C extends S {
cProps: string = "cProps"
}
type Predicate<T> = (value: T) => boolean;
// 1. Expected behavior
const cPredicate: Predicate<C> = (value: C) => value.cProps == "cProps"; // OK
const cSuperPredicate: Predicate<C> = (value: S) => value.sProps == "sProps"; // Should be OK
// const sPredicate: Predicate<S> = (value: C) => value.cProps == "cProps"; // Invalid
console.log("cPredicate", cPredicate(new C()))
console.log("cSuperPredicate", cSuperPredicate(new C()))
// console.log("sPredicate", sPredicate(new S()))
// 2. Expected Behavior
const some = <T>(...predictes: Predicate<T>[]): Predicate<T> =>
(value: T) =>
predictes.some(p => p(value))
// T is inferred as S
//
// below code is same as:
// console.log(some<S>(
// ^^^
console.log(some(
(value: S) => value.sProps == "sProps", // Should be OK
(value: C) => value.cProps == "cProps", // Invalid
)(new S()))
// console.log(some<S>(cPredicate, cSuperPredicate)(new C()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment