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

useState type differences between React and Preact #3181

Closed
1 task done
luhis opened this issue Jun 9, 2021 · 0 comments · Fixed by #3185
Closed
1 task done

useState type differences between React and Preact #3181

luhis opened this issue Jun 9, 2021 · 0 comments · Fixed by #3185
Labels

Comments

@luhis
Copy link

luhis commented Jun 9, 2021

  • Check if updating to the latest Preact version resolves the issue

Describe the bug
The type definitions for useState differ slightly.

React: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L913
Preact: https://github.com/preactjs/preact/blob/master/hooks/src/index.d.ts#L10

The React definitions allow for calling useState with no initialState, and it will infer a state of type T | undefined. In Preact I must pass in an undefined as the initialState, and create a union with undefined for the type.

Preact valid code:

const [editingObject, setEditingObject] = useState<{id: number} | undefined>(
        undefined
    );

To Reproduce

const [editingObject, setEditingObject] = useState<{ id: number }>();

Expected behavior
This should compile and infer a state of type { id: number } | undefined

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

Successfully merging a pull request may close this issue.

2 participants