Skip to content

Commit d22a170

Browse files
fix: Infer propertly the own props in complex polymorphic components
1 parent 0cf138e commit d22a170

File tree

5 files changed

+119
-102
lines changed

5 files changed

+119
-102
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [18.x]
15+
node-version: [18.x, 20.x, 22.x]
1616
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1717

1818
steps:

index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import type {
1010
} from 'react';
1111

1212
// Utility type to merge two types
13-
type Merge<T, U> = Omit<T, keyof U> & U;
13+
type Merge<T, U> = {
14+
[K in keyof T as K extends keyof U ? never : K]: T[K];
15+
} & U;
1416

1517
// Props type with an "as" prop that allows specifying the element type
1618
export type PropsWithAs<
@@ -42,14 +44,14 @@ export type PolymorphicExoticProps<
4244
P,
4345
T extends ElementType,
4446
S extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements,
45-
> = T extends ExoticComponent<infer U> ? PolymorphicProps<Merge<P, PropsWithoutAs<PropsWithoutRef<U>>>, T, S> : never;
47+
> = T extends ExoticComponent<infer U> ? PolymorphicProps<Merge<PropsWithoutAs<PropsWithoutRef<U>>, P>, T, S> : never;
4648

4749
// Polymorphic props type for functional components
4850
export type PolymorphicFunctionalProps<
4951
P,
5052
T extends ElementType,
5153
S extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements,
52-
> = T extends FC<infer U> ? PolymorphicProps<Merge<P, PropsWithoutAs<PropsWithoutRef<U>>>, T, S> : never;
54+
> = T extends FC<infer U> ? PolymorphicProps<Merge<PropsWithoutAs<PropsWithoutRef<U>>, P>, T, S> : never;
5355

5456
// Type for the forwarded ref of a component
5557
export type PolymorphicForwardedRef<C extends ElementType> = ComponentPropsWithRef<C>['ref'];

package-lock.json

Lines changed: 92 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242
"homepage": "https://github.com/axa-ch/react-polymorphic-types#readme",
4343
"devDependencies": {
4444
"@axa-ch/easy-config": "2.1.1",
45-
"@biomejs/biome": "1.8.3",
46-
"framer-motion": "^11.5.4",
45+
"@biomejs/biome": "1.9.3",
46+
"framer-motion": "^11.11.7",
4747
"prettier": "3.3.3",
48-
"typescript": "^5.6.2"
48+
"typescript": "^5.6.3"
4949
},
5050
"dependencies": {
51-
"@types/react": "^18.3.5",
51+
"@types/react": "^18.3.11",
5252
"react": "^18.3.1"
5353
}
5454
}

0 commit comments

Comments
 (0)