[Epic] TypeScript refactor
See original GitHub issueObjective
Rewrite Primer React components in TypeScript.
📄 TypeScript ADR 📄 TypeScript notes google doc
Team
- DRI: @colebemis
- Engineering: @colebemis @smockle @bscofield @VanAnderson
- Product: n/a
- Design: n/a
Tasks
Phase 1: Setup
- Support
.ts
and.tsx
files https://github.com/primer/components/pull/959 - Type check test files https://github.com/primer/components/pull/971
Phase 2: Migrate
- Migrate components¹
In Review or Done
- Dialog @VanAnderson https://github.com/primer/components/pull/1060
- Dropdown @colebemis https://github.com/primer/components/pull/1059
- theme @colebemis https://github.com/primer/components/pull/1058
- utils @VanAnderson https://github.com/primer/components/pull/1055
- SelectMenu @colebemis https://github.com/primer/components/pull/1048
- Pagination @VanAnderson https://github.com/primer/components/pull/1047
- PointerBox @colebemis https://github.com/primer/components/pull/1042
- SideNav @VanAnderson https://github.com/primer/components/pull/1040
- SubNav @VanAnderson https://github.com/primer/components/pull/1038
- Timeline @colebemis https://github.com/primer/components/pull/1037
- CircleOcticon @colebemis https://github.com/primer/components/pull/1036
- CircleBadge @colebemis https://github.com/primer/components/pull/1035
- Popover @colebemis https://github.com/primer/components/pull/1034
- Tooltip @colebemis https://github.com/primer/components/pull/1033
- AvatarStack @colebemis https://github.com/primer/components/pull/1032
- TabNav @VanAnderson https://github.com/primer/components/pull/1029
- AvatarPair @colebemis https://github.com/primer/components/pull/1031
- Caret @colebemis https://github.com/primer/components/pull/1030
- TextInput @colebemis https://github.com/primer/components/pull/1028
- Details @VanAnderson https://github.com/primer/components/pull/1026
- Button @smockle https://github.com/primer/components/pull/1017
- Header @VanAnderson https://github.com/primer/components/pull/1020
- FilterList @colebemis https://github.com/primer/components/pull/1015
- UnderlineNav @colebemis https://github.com/primer/components/pull/1014
- Breadcrumb @colebemis https://github.com/primer/components/pull/1012
- Link @VanAnderson https://github.com/primer/components/pull/1011
- StateLabel @colebemis https://github.com/primer/components/pull/1008
- FilteredSearch @colebemis https://github.com/primer/components/pull/1006
- CounterLabel @colebemis https://github.com/primer/components/pull/1005
- Truncate @colebemis https://github.com/primer/components/pull/1003
- FormGroup @colebemis https://github.com/primer/components/pull/999
- Position @colebemis https://github.com/primer/components/pull/998
- Flash @colebemis https://github.com/primer/components/pull/995
- StyledOcticon @colebemis https://github.com/primer/components/pull/993
- ProgressBar @colebemis https://github.com/primer/components/pull/987
- Avatar @bscofield https://github.com/primer/components/pull/984
- Grid @colebemis https://github.com/primer/components/pull/989
- Flex @colebemis https://github.com/primer/components/pull/986
- BorderBox #973
- Text #979
- BaseStyles #982
- Box #959
- BranchName #974
- Heading #975
- Label #976
- LabelGroup #976
- Pagehead #977
Phase 3: Clean up
- Remove ambient declaration file (
index.d.ts
) https://github.com/primer/components/pull/1147 - Remove propTypes https://github.com/primer/components/pull/1068
Postponed
- Update contributor guidelines
- Remove default exports
- Generate prop documentation https://github.com/primer/components/issues/1109
¹How to migrate a component to TypeScript
The migration process for each component may vary, but it will likely follow these high-level steps:
1. Change the file extension from .js
to .tsx
src/Box.js → src/Box.tsx
2. Add type annotations to the component file
Here are type annotations for the Box
component:
// src/Box.tsx
import styled from 'styled-components'
+ import {COMMON, FLEX, LAYOUT, SystemCommonProps, SystemFlexProps, LayoutProps} from './constants'
+ import sx, {SxProp} from './sx'
+ import {ComponentProps} from './utils/types'
+ const Box = styled.div<CommonProps & FlexProps & LayoutProps & SxProp>`
${COMMON}
${FLEX}
${LAYOUT}
${sx}
`
+ export type BoxProps = ComponentProps<typeof Box>
export default Box
Most Primer React components will follow a similar pattern. Each component should export a ___Props
type in addition to the default export.
3. Change the file extension of tests
src/__tests__/Box.js → src/__tests__/Box.tsx
👉 See the TypeScript notes google doc for more information on TypeScript migration.
Exit criteria
- All JavaScript files in the
src
directory of primer/components have been rewritten in TypeScript
/cc @philipbremer
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Epic Maintenance task: Refactor and clean up stuff · Issue #2883 ...
I believe it is a time to refactor stuff so we are not lost in the future. Stuff to be considered: reconsidering what...
Read more >redux-observable epic typescript warning: 'store' is declared ...
It's a linting error from TypeScript noUnusedParameters aka no-unused-variable setting. It means that your function has the store parameter ...
Read more >Fully Typed Web Apps | Epic Web Dev by Kent C. Dodds
TypeScript is an enormous part of the web industry. ... Without types guiding you through that refactor you're gonna have a hard time....
Read more >Refactoring TypeScript: Keeping your code healthy
Refactoring TypeScript : Keeping your code healthy [Hickey, James] on Amazon.com. *FREE* shipping on qualifying offers. Refactoring TypeScript: Keeping your ...
Read more >Refactor Redux Reducers Like a Pro With TypeScript
TypeScript is a perfect match for React reducers because it has an intelligent type system, making refactoring much easier than it is with ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
4/1 Update
Status 💚
Ships
This week we replaced our hand-written type definitions with type definitions generated by the TypeScript compiler (https://github.com/primer/components/pull/1147). This means consumers get more accurate type definitions and maintainers no longer have to manually keep the type definitions in sync with the code! This is the culmination of a quarter-long effort to rewrite Primer React in TypeScript 🎉 (https://github.com/primer/components/issues/970). These changes will be included in the next release of Primer React.
Up Next
Status
💛 Yellow
Blockers
The final step of this TypeScript refactor is to replace our manually maintained type definitions with type definitions generated by TypeScript. However, after testing in Memex, I found that the generated type definitions were causing errors because of the way the
as
prop is defined.I figured out a way to correctly define types for the
as
prop but it will require us to update all our components:Here's an example of how our components will need to be updated
I haven’t had time to completely implement this solution because of Primer React theming work. I’ll reach out to @VanAnderson to see if he as the bandwidth to take this on.
Up Next
as
prop types so we can replace our manually maintained type definitions with type definitions generated by TypeScript