Skip to content

Commit

Permalink
Relaxed types for shouldForwardProp
Browse files Browse the repository at this point in the history
This function needs to be able to filter a props for a generic argument of the resulting function.
  • Loading branch information
Jake Ginnivan committed Nov 18, 2019
1 parent f1b7c9d commit efe168e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-candles-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/is-prop-valid': minor
---

[TypeScript] Allow isPropValid to take any PropertyKey as an argument (instead of just string)
7 changes: 7 additions & 0 deletions .changeset/dull-carrots-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@emotion/styled': patch
---

Relaxed types for shouldForwardProp

This function needs to be able to filter a props for a generic argument of the resulting function.
2 changes: 1 addition & 1 deletion packages/is-prop-valid/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
14 changes: 14 additions & 0 deletions packages/styled/types/tests-base.tsx
Original file line number Diff line number Diff line change
@@ -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,19 @@ const Canvas1 = styled('canvas', {
;<Canvas0 id="id-should-be-filtered" />
;<Canvas1 />

const styledWithForwardedExtraProp = styled('div', {
// this used to work, but now complains because 'priority' is not a prop of '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 efe168e

Please sign in to comment.