cva: TS2742 - inferred type cannot be named without a reference
Describe the bug
The following TS error is encountered when attempting to build a library with class-variance-authority@0.6.0:
The inferred type of 'someCVAVariable' cannot be named without a reference to '../../../node_modules/class-variance-authority/dist/types'. This is likely not portable. A type annotation is necessary.ts(2742)
Similar to issue: https://github.com/joe-bell/cva/issues/27
TypeScript version: 5.0.4
To Reproduce Steps to reproduce the behavior:
- Use TSConfig with
declaration: truesetting. - Create an export utility that uses
VariantPropsand calls a declaredcva()function. See screenshot.
Expected behavior No TS Error.
Screenshots
TypeScript Version 5.0.4
Additional context
The problem is that ClassProp, ClassValue, OmitUndefined, StringToBoolean types are not ultimately exported from the class-variance-authority library. If you look at the index.d.ts file you can see those types are imported and referenced in other types, but they ultimately aren’t exported themselves (see the last line in of index.d.ts of export {}.
Simply exporting those types fixes the issue.
ie
diff --git a/dist/index.d.ts b/dist/index.d.ts
index 676e466a43ad8932cbb3131bb2c3dea687d47041..26e2c538a42fff5ab15a6e584c48ce6001fda86a 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -18,4 +18,4 @@ type Config<T> = T extends ConfigSchema ? {
} : never;
type Props<T> = T extends ConfigSchema ? ConfigVariants<T> & ClassProp : ClassProp;
export declare const cva: <T>(base?: ClassValue, config?: Config<T> | undefined) => (props?: Props<T> | undefined) => string;
-export {};
+export { ClassProp, ClassValue, OmitUndefined, StringToBoolean };
Happy to submit a PR.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 17 (11 by maintainers)
Some good news, I’ve managed to resolve this issue by 2 steps that seem to be interlinked:
index.tsclsx’s types rather than importing themBefore I commit to this strategy, I want to see if using
@JSDocsuffers from the same issue