Skip to content

Commit

Permalink
Relaxed types for shouldForwardProp (#1643)
Browse files Browse the repository at this point in the history
* Relaxed types for shouldForwardProp

This function needs to be able to filter a props for a generic argument of the resulting function.

* Update .changeset/dull-carrots-enjoy.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update .changeset/dull-carrots-enjoy.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update .changeset/cool-candles-lie.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
JakeGinnivan and Andarist committed Nov 19, 2019
1 parent cbb8b79 commit 923ded0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-candles-lie.md
@@ -0,0 +1,5 @@
---
'@emotion/is-prop-valid': minor
---

Allow `isPropValid` to take any `PropertyKey` as an argument (instead of just `string`).
6 changes: 6 additions & 0 deletions .changeset/dull-carrots-enjoy.md
@@ -0,0 +1,6 @@
---
'@emotion/styled': patch
---

Relaxed types for `shouldForwardProp` as it needs to be able to filter props for a generic argument of the resulting function.

2 changes: 1 addition & 1 deletion packages/is-prop-valid/types/index.d.ts
@@ -1,5 +1,5 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.1

declare function isPropValid(string: string): boolean
declare function isPropValid(string: PropertyKey): boolean
export default isPropValid
2 changes: 0 additions & 2 deletions packages/is-prop-valid/types/tests.ts
Expand Up @@ -5,8 +5,6 @@ isPropValid('ref')
// $ExpectError
isPropValid()
// $ExpectError
isPropValid(5)
// $ExpectError
isPropValid({})
// $ExpectError
isPropValid('ref', 'def')
4 changes: 2 additions & 2 deletions packages/styled/types/base.d.ts
Expand Up @@ -31,13 +31,13 @@ export interface FilteringStyledOptions<
ForwardedProps extends keyof Props = keyof Props
> {
label?: string
shouldForwardProp?(propName: keyof Props): propName is ForwardedProps
shouldForwardProp?(propName: PropertyKey): propName is ForwardedProps
target?: string
}

export interface StyledOptions<Props> {
label?: string
shouldForwardProp?(propName: keyof Props): boolean
shouldForwardProp?(propName: PropertyKey): boolean
target?: string
}

Expand Down
13 changes: 13 additions & 0 deletions packages/styled/types/tests-base.tsx
@@ -1,5 +1,6 @@
import * as React from 'react'
import styled from '@emotion/styled/base'
import isPropValid from '@emotion/is-prop-valid'

// tslint:disable-next-line: interface-over-type-literal
type ReactClassProps0 = {
Expand Down Expand Up @@ -98,6 +99,18 @@ const Canvas1 = styled('canvas', {
;<Canvas0 id="id-should-be-filtered" />
;<Canvas1 />

const styledWithForwardedExtraProp = styled('div', {
shouldForwardProp: prop => prop !== 'priority' && isPropValid(prop)
})

type Priority = 'info' | 'warning' | 'error'

const Alert = styledWithForwardedExtraProp<{
priority?: Priority
}>(({ priority, theme }) => ({
backgroundColor: theme.colors[priority || 'info']
}))

interface PrimaryProps {
readonly primary: string
}
Expand Down

0 comments on commit 923ded0

Please sign in to comment.