Skip to content

Commit fc4d7bd

Browse files
authored
Convert @emotion/react's source code to TypeScript (#3281)
* Convert `@emotion/react`'s source code to TypeScript * add changeset * limit `package.json['files']` * fixed type issue * try `esModuleInterop` * add required cast * use `resolveJsonModule` too * fixed extra things * fixed a silly mistake * one extra thing * fixed type error * try building before running dtslint * fixed import * try to bump tested TS versions * add extra ts-ignore * make sure the ts-ignore is above the problematic line * rewrite tests to stop relying on implementation detail * fixed type error * corrext tests * fixed entrypoints types
1 parent 8dc1a6d commit fc4d7bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+582
-585
lines changed

.changeset/rotten-baboons-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/react': minor
3+
---
4+
5+
Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,8 @@ jobs:
8888
- uses: actions/checkout@v3
8989
- uses: ./.github/actions/ci-setup
9090

91+
- name: build
92+
run: yarn build
93+
9194
- name: dtslint
9295
run: yarn test:typescript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
"@testing-library/react": "13.0.0-alpha.5",
189189
"@types/jest": "^29.5.12",
190190
"@types/node": "^12.20.37",
191-
"@types/react": "18.2.6",
191+
"@types/react": "18.3.12",
192192
"@typescript-eslint/eslint-plugin": "^7.13.0",
193193
"@typescript-eslint/parser": "^7.13.0",
194194
"babel-check-duplicated-nodes": "^1.0.0",

packages/cache/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from '../src'
1+
export * from '..'

packages/css/test/no-babel/index.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import styled from '@emotion/styled'
77
let consoleError = console.error
88

99
afterEach(() => {
10-
// $FlowFixMe
1110
console.error = consoleError
1211
})
1312

@@ -146,7 +145,6 @@ describe('css', () => {
146145
})
147146

148147
const spy = jest.fn()
149-
// $FlowFixMe
150148
console.error = spy
151149

152150
expect(() =>

packages/css/test/sheet.dom.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { sheet } from '@emotion/css'
33
const consoleError = console.error
44

55
afterEach(() => {
6-
// $FlowFixMe
76
console.error = consoleError
87
})
98

@@ -38,7 +37,6 @@ describe('sheet', () => {
3837
test('throws', () => {
3938
sheet.speedy(true)
4039
const spy = jest.fn()
41-
// $FlowFixMe
4240
console.error = spy
4341
sheet.insert('.asdfasdf4###112121211{')
4442
expect(spy.mock.calls.length).toBe(1)

packages/native/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Definitions by: Pat Sissons <https://github.com/patsissons>
2-
// TypeScript Version: 3.4
2+
// TypeScript Version: 4.1
33

44
import * as RN from 'react-native'
55
import { Theme } from '@emotion/react'

packages/native/types/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"jsx": "react",
66
"lib": ["es6", "dom"],
77
"module": "commonjs",
8+
"esModuleInterop": true,
9+
"resolveJsonModule": true,
810
"noEmit": true,
911
"strict": true,
1012
"target": "es5",

packages/primitives-core/src/styled.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export function createStyled(
5151

5252
// do we really want to use the same infra as the web since it only really uses theming?
5353
let Styled = React.forwardRef<unknown, StyledProps>((props, ref) => {
54-
const finalTag = (shouldUseAs && props.as) || component
54+
const finalTag =
55+
(shouldUseAs && (props.as as React.ElementType)) || component
5556

5657
let mergedProps = props
5758
if (props.theme == null) {

packages/react/__tests__/element.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
// @flow
21
/** @jsx jsx */
32
import { render } from '@testing-library/react'
43
import { jsx, css, CacheProvider, ThemeProvider } from '@emotion/react'
54
import createCache from '@emotion/cache'
65

7-
// $FlowFixMe
86
console.error = jest.fn()
97

108
beforeEach(() => {
11-
// $FlowFixMe
129
document.head.innerHTML = ''
1310
jest.clearAllMocks()
1411
})
@@ -18,7 +15,6 @@ describe('EmotionElement', () => {
1815
const theme = { color: 'blue' }
1916
const cache = createCache({ key: 'context' })
2017

21-
// $FlowFixMe
2218
const Comp = ({ flag }) => (
2319
<ThemeProvider theme={theme}>
2420
<CacheProvider value={cache}>

0 commit comments

Comments
 (0)