Skip to content

Instantly share code, notes, and snippets.

@raould
Created January 5, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raould/645c93035adfd479b1801d2c539ee55e to your computer and use it in GitHub Desktop.
Save raould/645c93035adfd479b1801d2c539ee55e to your computer and use it in GitHub Desktop.
// 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