Created
May 24, 2020 15:16
-
-
Save syusui-s/a1874a4a91e502f1a6b602ad455cacde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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