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

yup.setLocale() types #1321

Closed
dahaca opened this issue Mar 30, 2021 · 3 comments · Fixed by #1427
Closed

yup.setLocale() types #1321

dahaca opened this issue Mar 30, 2021 · 3 comments · Fixed by #1427

Comments

@dahaca
Copy link

dahaca commented Mar 30, 2021

After upgrading the library and removing @types/yup to enable native TS support I've encountered a problem with yup.setLocale(). I am having issues with the following code:

yup.setLocale({
  string: {
    email: 'string.email',
    min: params => ({
      text: 'string.min',
      params,
    }),
    max: params => ({
      text: 'string.max',
      params,
    }),
  },
  number: {
    min: params => ({
      text: 'number.min',
      params,
    }),
    max: params => ({
      text: 'number.max',
      params,
    }),
  },
})

The approach is taken straight from the docs. So why is the type setLocale(custom: any): void? Won't params.min or params.type be present by default? Why those are not included. Yes, I can easily type it myself but it seems weird.

Are there plans for typing it and if not - why not?

Thanks!

@StevenVerbiest
Copy link

StevenVerbiest commented May 27, 2021

I'm having the same issue. Typing used to be defined as LocaleObject, but is now just any.

Old type:

interface LocaleObject {
    mixed?: MixedLocale;
    string?: StringLocale;
    number?: NumberLocale;
    date?: DateLocale;
    boolean?: {};
    object?: ObjectLocale;
    array?: ArrayLocale;
}

@akafaneh
Copy link

akafaneh commented Jul 6, 2021

Same need here, for now i'm using patch-package to add the removed types manually, until it get supported. I copied the type from the definitelytyped package that @StevenVerbiest mentioned.

@maurodesouza
Copy link
Contributor

maurodesouza commented Jul 19, 2021

Hi!

For now, is also possible to overload the current type of setLocale with custom type definitions

// yup.d.ts

import {
  ArrayLocale,
  BooleanLocale,
  DateLocale,
  MixedLocale,
  NumberLocale,
  ObjectLocale,
  StringLocale,
} from 'yup/lib/locale';

declare module 'yup' {
  export interface LocaleObject {
    mixed?: MixedLocale;
    string?: StringLocale;
    number?: NumberLocale;
    date?: DateLocale;
    boolean?: BooleanLocale;
    object?: ObjectLocale;
    array?: ArrayLocale;
  }

  export function setLocale(custom: LocaleObject): void;
}

The types of setLocale will works fine!

maurodesouza added a commit to maurodesouza/yup that referenced this issue Jul 23, 2021
The setLocale file was changed to .ts and the LocaleObject interface was created in locale.ts

Close jquense#1321.
jquense pushed a commit that referenced this issue Jul 26, 2021
The setLocale file was changed to .ts and the LocaleObject interface was created in locale.ts

Close #1321.
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

Successfully merging a pull request may close this issue.

4 participants