Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

feat(types): improve & add type declarations #473

Merged
merged 5 commits into from
Aug 13, 2021

Conversation

Pwuts
Copy link
Contributor

@Pwuts Pwuts commented Aug 8, 2021

Summary

  • Fixed type declaration of theme and moved it to chakra-ui-theme
  • Fixed type declaration of chakra plugin options
  • Added global instance type extensions for Vue & Nuxt (for $toast among other things)

Motivation and Context

Since this library isn't natively typed, it is hard to use in conjunction with typescript (e.g. Nuxt). I have attempted to somewhat improve the situation.

Partially fixes #469, and has potential to solve it entirely: depending on the response to this PR, I can also add $chakraTheme, $chakraColorMode etc quite easily.

How Has This Been Tested?

I used the example Nuxt app in chakra-ui-nuxt/example and locally amended it to do type checking (by adding lang="ts"), and this.$toast is now correctly recognized. Its options are also correctly auto-completed. I do not get any new errors, other than the errors emitted by yarn test:module in the latest version of the develop branch.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@changeset-bot
Copy link

changeset-bot bot commented Aug 8, 2021

⚠️ No Changeset found

Latest commit: 1e37eb9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Aug 8, 2021

@Pwuts is attempting to deploy a commit to the Chakra UI Team on Vercel.

A member of the Team first needs to authorize it.

@Pwuts
Copy link
Contributor Author

Pwuts commented Aug 8, 2021

I'm not sure about the code style of the project. The project originally barely contains any typescript, and neither ESLint nor Pretty complained about the code I wrote. Does this mean my code conforms to the project's code style?

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 8, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1e37eb9:

Sandbox Source
Chakra UI Vue (0.X) Starter Template Configuration
chakra-ui-nuxt-demo Configuration

@vercel
Copy link

vercel bot commented Aug 13, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

chakra-ui-vue – ./

🔍 Inspect: https://vercel.com/chakra-ui/chakra-ui-vue/9sM7SByxR1miMTCb9XWbxLKVYJgp
✅ Preview: https://chakra-ui-vue-git-fork-pwuts-improve-types-chakra-ui.vercel.app

[Deployment for 1e37eb9 failed]

@codebender828
Copy link
Collaborator

codebender828 commented Aug 13, 2021

I'm not sure about the code style of the project. The project originally barely contains any typescript, and neither ESLint nor Pretty complained about the code I wrote. Does this mean my code conforms to the project's code style?

Thanks for this PR!

About the linting, if it doesn't complain, then it most likely is good. We run checks in front of each commit, and when you open a pull request. :)

Copy link
Collaborator

@codebender828 codebender828 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this pull request @Pwuts Will add this to the next minor release

Comment on lines +1 to +23
export type Theme = {
breakpoints: { [key: string]: string }
zIndices: { [key: string]: string | number }
radii: { [key: string]: string }
opacity: { [key: string]: string }
borders: { [key: string]: string }
colors: { [key: string]: string | { [opacity: string]: string }}
fonts: {
heading: string
body: string
mono: string
}
fontSizes: { [key: string]: string }
fontWeights: { [key: string]: number }
letterSpacings: { [key: string]: string }
lineHeights: { [key: string]: string }
borderWidths: { [key: string]: string }
shadows: { [key: string]: string }
sizes: { [key: string]: string }
space: { [key: string]: string }
}

declare const theme: Theme
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Pwuts Is it possible that we can derive a deep Partial from the original theme object here? Something like:

import theme from './src`

export type Theme = DeepPartial<typeof theme>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the definition of Theme shouldn't be partial, but it could be derived from the default theme, sure.

@Pwuts
Copy link
Contributor Author

Pwuts commented Aug 13, 2021

@codebender828 epic! I'll look into doing something similar for the theme and color mode providers, although that's not as straightforward since they are provider components and don't inject into the global Vue instance.

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

Successfully merging this pull request may close these issues.

qn: Error using $chakraColorMode method with typescript
2 participants