Skip to content

Instantly share code, notes, and snippets.

@tai2
Created November 20, 2017 12:00
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 tai2/8a61a2789164128e2c17d069dea9dab9 to your computer and use it in GitHub Desktop.
Save tai2/8a61a2789164128e2c17d069dea9dab9 to your computer and use it in GitHub Desktop.
Extract Props from given ComponentClass object
import * as React from 'react';
// Type utilities
type DiffKey<T extends string, U extends string> = (
& {[P in T]: P } // (1)
& {[P in U]: never } // (2)
& { [x: string]: never } // (3)
)[T]; // (4)
type Omit<T, K extends keyof T> = Pick<T, DiffKey<keyof T, K>>;
// Target type extracted from ComponentClass
type Props = {
x: string
};
// Assuming ComponentClass C is given from somewhere else.
const C : React.ComponentClass<Props> = {} as React.ComponentClass<Props>;
const c = new C({x: 'aaa'});
type T = Omit<typeof c.props, 'children'>;
// T is equivalent to Props
const a : T = {
x: 'xxx',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment