-
-
Save tai2/8a61a2789164128e2c17d069dea9dab9 to your computer and use it in GitHub Desktop.
Extract Props from given ComponentClass object
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
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