Skip to content

Commit

Permalink
Tweak type-level tests around shouldForwardProp
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed May 30, 2022
1 parent fce092c commit 59e8aec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/native/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export type Interpolation<
* Practical sense: you can define and reuse atomic `shouldForwardProp` filters that are strictly bound with some `ForwardedProps` type.
*/
export interface FilteringStyledOptions<
Props = {},
Props = Record<string, any>,
ForwardedProps extends keyof Props = keyof Props
> {
shouldForwardProp?: (propName: PropertyKey) => propName is ForwardedProps
shouldForwardProp?: (propName: string) => propName is ForwardedProps & string
}

export interface StyledOptions<Props = {}> {
shouldForwardProp?: (propName: PropertyKey) => boolean
export interface StyledOptions<Props = Record<string, any>> {
shouldForwardProp?: (propName: string) => boolean
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/native/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,15 @@ export const ImageFullWidthContained = styled.Image`
{
// Props forwarding through StyledOptions and FilteringStyledOptions

styled(View, { shouldForwardProp: (prop: string) => true })({})
// $ExpectError
styled(View, { shouldForwardProp: (prop: 'testID') => true })({})

styled(View, {
shouldForwardProp: (prop: string): prop is 'testID' => true
})({})
styled(View, {
// $ExpectError
shouldForwardProp: (prop: 'testID'): prop is 'testID' => true
})({})

Expand Down
10 changes: 4 additions & 6 deletions packages/styled/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ export { ComponentSelector, Interpolation }
* Practical sense: you can define and reuse atomic `shouldForwardProp` filters that are strictly bound with some `ForwardedProps` type.
*/
export interface FilteringStyledOptions<
Props = {},
Props = Record<string, any>,
ForwardedProps extends keyof Props = keyof Props
> {
label?: string
// we can't use `keyof Props` here because we need to include `AdditionalProps` and those aren't available yet
// `Props` represent the actual props of a component while `AdditionalProps` represent props used only for styling purposes
shouldForwardProp?: (propName: PropertyKey) => propName is ForwardedProps
shouldForwardProp?: (propName: string) => propName is ForwardedProps & string
target?: string
}

export interface StyledOptions<Props = {}> {
export interface StyledOptions<Props = Record<string, any>> {
label?: string
shouldForwardProp?: (propName: PropertyKey) => boolean
shouldForwardProp?: (propName: string) => boolean
target?: string
}

Expand Down
23 changes: 16 additions & 7 deletions packages/styled/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,16 @@ const Input5 = styled.input`

const fc: React.FC<{ foo: string }> = () => null

// we can't accept a "known" prop here because we need to include `AdditionalProps` and those aren't available yet
// `Props` represent the actual props of a component while `AdditionalProps` represent props used only for styling purposes
// $ExpectError
styled(fc, { shouldForwardProp: (prop: 'foo') => true })({})

styled(fc, { shouldForwardProp: (prop: string) => true })({})

// $ExpectError
styled(fc, { shouldForwardProp: (prop: 'bar') => true })({})

// $ExpectError
styled(fc, { shouldForwardProp: (prop: 'foo') => true })({})

// $ExpectError
Expand All @@ -246,6 +249,8 @@ const Input5 = styled.input`
foo: string
}>['shouldForwardProp'] = (prop: 'unknown') => true

styled(fc, { shouldForwardProp: (prop: string): prop is 'foo' => true })({})
// $ExpectError
styled(fc, { shouldForwardProp: (prop: 'foo'): prop is 'foo' => true })({})

const shouldForwardProp3: FilteringStyledOptions['shouldForwardProp'] = (
Expand All @@ -259,33 +264,37 @@ const Input5 = styled.input`

const shouldForwardProp5: FilteringStyledOptions<{
foo: string
}>['shouldForwardProp'] = (prop: string): prop is 'foo' => true
// $ExpectError
const shouldForwardProp6: FilteringStyledOptions<{
foo: string
}>['shouldForwardProp'] = (prop: 'foo'): prop is 'foo' => true

// $ExpectError
const shouldForwardProp6: FilteringStyledOptions<{
const shouldForwardProp7: FilteringStyledOptions<{
foo: string
}>['shouldForwardProp'] = (prop: 'unknown'): prop is 'unknown' => true

const shouldForwardProp7: FilteringStyledOptions<
const shouldForwardProp8: FilteringStyledOptions<
{ foo: string; bar: string },
'foo'
>['shouldForwardProp'] = (prop: 'foo' | 'bar'): prop is 'foo' => true
>['shouldForwardProp'] = (prop: string): prop is 'foo' => true

// $ExpectError
const shouldForwardProp8: FilteringStyledOptions<
const shouldForwardProp9: FilteringStyledOptions<
{ foo: string; bar: string },
'foo'
>['shouldForwardProp'] = (prop: 'foo' | 'bar'): prop is 'bar' => true

styled('div', {
shouldForwardProp: (prop: keyof JSX.IntrinsicElements['div']) => true
shouldForwardProp: (prop: string) => true
})({})

// $ExpectError
styled('div', { shouldForwardProp: (prop: 'color') => true })({})

// $ExpectError
styled('div', {
// $ExpectError
shouldForwardProp: (prop: 'color'): prop is 'color' => true
})({})

Expand Down

0 comments on commit 59e8aec

Please sign in to comment.