Created
January 5, 2020 15:25
-
-
Save raould/645c93035adfd479b1801d2c539ee55e to your computer and use it in GitHub Desktop.
This file contains 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
// see https://repl.it/repls/AnimatedPerfectMinimalsystem | |
// 1 Transform the type to flag all the undesired keys as 'never' | |
type FlagExcludedType<Base, Type> = { [Key in keyof Base]: Base[Key] extends Type ? never : Key }; | |
// 2 Get the keys that are not flagged as 'never' | |
type AllowedNames<Base, Type> = FlagExcludedType<Base, Type>[keyof Base]; | |
// 3 Use this with a simple Pick to get the right interface, excluding the undesired type | |
type OmitType<Base, Type> = Pick<Base, AllowedNames<Base, Type>>; | |
// 4 Exclude the Function type to only get properties | |
type ConstructorType<T> = OmitType<T, Function>; | |
interface X { | |
duration_msec: number; | |
available(now: number): boolean; | |
} | |
let x:ConstructorType<X> = { | |
duration_msec: 2, | |
}; | |
let y:X = { | |
...x, | |
available(now: number): boolean { return false; } | |
} | |
console.log("x", x); | |
console.log("y", y); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment