Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: Type of Reference is unknown #1232

Closed
zmtmaster opened this issue Jan 21, 2021 · 3 comments
Closed

Typescript: Type of Reference is unknown #1232

zmtmaster opened this issue Jan 21, 2021 · 3 comments

Comments

@zmtmaster
Copy link

Describe the bug
A possible bug with type of Reference.

To Reproduce

const validationSchema = yup.object().shape({
  fromAmount: yup.number().lessThan(9 * 10e9, "AMOUNT_IS_TOO_LARGE"),
  toAmount: yup
    .number()
    .lessThan(9 * 10e9, "AMOUNT_IS_TOO_LARGE")
    .moreThan(yup.ref('fromAmount'), "WRONG_RANGE_INPUT"),
});

yup.ref throws TS errors since its its unknown.

Expected behavior
Expected is not to throw TS error.

Additional context
Since Reference is typed already, adding generic type to the create function seems to solve this.

return new Reference(key, options);

export function create<T = unknown>(key: string, options?: ReferenceOptions) {
  return new Reference<T>(key, options);
}

Also this fix can be applicable using @types/yup.
Here is a patch-package solution

diff --git a/node_modules/yup/lib/Reference.d.ts b/node_modules/yup/lib/Reference.d.ts
index be5b8f3..645a98f 100644
--- a/node_modules/yup/lib/Reference.d.ts
+++ b/node_modules/yup/lib/Reference.d.ts
@@ -2,7 +2,7 @@ import type { SchemaRefDescription } from './schema';
 export declare type ReferenceOptions<TValue = unknown> = {
     map?: (value: unknown) => TValue;
 };
-export declare function create(key: string, options?: ReferenceOptions): Reference<unknown>;
+export declare function create<T = unknown>(key: string, options?: ReferenceOptions): Reference<T>;
 export default class Reference<TValue = unknown> {
     readonly key: string;
     readonly isContext: boolean;
@Dacerwei
Copy link

Dacerwei commented Feb 5, 2021

got same type error thanks!
the solution works for me by editing node_module file
could you explain more about patch-package example?

@zmtmaster
Copy link
Author

zmtmaster commented Feb 5, 2021

@Dacerwei

got same type error thanks!
the solution works for me by editing node_module file
could you explain more about patch-package example?

patch-package is an NPM module I use it to patch module until a PR was approved.

"patch-package lets app authors instantly make and keep fixes to npm dependencies .....No more waiting around for pull requests to be merged and published. No more forking repos just to fix that one tiny thing preventing your app from working..."

@zmtmaster
Copy link
Author

@Dacerwei A fix was made already I think, see issue #1208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants