Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript friendliness #31

Open
yang opened this issue Jul 8, 2018 · 5 comments
Open

Typescript friendliness #31

yang opened this issue Jul 8, 2018 · 5 comments

Comments

@yang
Copy link

yang commented Jul 8, 2018

Since this dynamically changes component signatures, I'm wondering what the easiest approach is to get the correct Typescript types on wrapper components - I'm guessing you just need to redeclare the prop types of the wrapper component, but wanted to see if I'm missing anything. Thanks!

@otraghly
Copy link

otraghly commented Sep 21, 2018

I've made simple wrapper:
uncontrollabe_ts

Could be used like so:

// props which example Component will share with uncontrollable
interface ControllableProps {
    value?: string
    on_value_change: (value: string) => void
}

// Props of the example Component (extends ControllableProps)
interface Props extends ControllableProps {
    color: string
    autoFocus?: boolean
}

class Example extends React.Component<Props> {
... Here goes the component
}

// Make type with all props handler by uncontrollable (extends ControllableProps)
interface UncontrollableProps extends ControllableProps {
    defaultValue?: string
}

// Resulting component:
export default Uncontrollable<Props, UncontrollableProps>({
    value: 'on_value_change',
})(Example)

@otraghly
Copy link

Hi guys, it's me again.
I rewrited the wrapper https://github.com/otraghly/uncontrollabe_ts, made it much simplier to use. No more extends and typecasts (well, almost). Look:

// Tested on TS 3.0.3
//
// The Props interface should specify all props of the component,
// AND default*-like props for uncontrollable.
//
// Note that on_change- like handlers is mandatory, value- and default*- are optional.
interface Props {
    // First props set
    value?: number // Optional
    on_change: (value: number) => void // Mandatory, as uncontrollable guarantees presence (or TS will force you to ckeck before use.)
    defaultValue?: number // Though extendable component will not see it. 

    // Second set
    title?: string
    on_title_change: (t: string) => void
    defaultTitle?: string

    // Other props, whatever you have.
    other: boolean
    another?: string
}

class Example extends React.Component<Props> {
    public render () { return null }
}

// It's crucial to pass values typed as string literals, otherwise it won't work!
export default Uncontrollable({
    value: 'on_change' as 'on_change', // As literal!
    title: 'on_title_change' as 'on_title_change', // As literal!
})(Example)

@alex-e-leon
Copy link

I've created an attempt to type the HOC in definitely typed here:
DefinitelyTyped/DefinitelyTyped#50115

It could likely use some improvements, but since typescript 4.1 introduces template literals to TS it should be possible to completely type the HOC now

@jquense
Copy link
Owner

jquense commented Dec 14, 2020

ya'll I recommend people use the hook, not the HOC for nicer TS support. It's API is designed to be easier to type amoung other considerations. Im also more than happy to accept PRs to improve the typing for the HOC

@alex-e-leon
Copy link

Out of curiosity outside of typing what's the reason for preferring the hook?

Personally I like how much simpler the API is with the HOC - as it's just one line of code, and if the types can be generated from the HOC it means you don't need to worry about typing anything but the controlled component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants