Yup: [**NOT RESOLVED**][Typing] Nested schemas properties are marked as any

Created on 2 Jul 2020  路  12Comments  路  Source: jquense/yup

Describe the bug
This is using the @types/yup package.
Define the following schema:

export const test = object({
  prop: string(),
  otherProp: object({
    subProp: string(),
  })
});

When compiled, this will output a .d.ts file with the following:

export declare const test: import("yup").ObjectSchema<{
    prop: string | undefined;
    otherProp: {
        subProp: any;
    } | undefined;
} | undefined>;

To Reproduce
View the code sandbox.
To rebuild the Typescript, you can open a new terminal and type yarn build.
https://codesandbox.io/s/yup-type-problem-inxy7

Expected behavior
The nested properties are typed properly.

All 12 comments

Update: I found that forcing the type to a Schema worked.

export const test = object({
  prop: string(),
  otherProp: object({
    subProp: string(),
  }) as Schema<{ subProp: string; }>
});

This tells me that the compiler is probably not able to infer the types in ObjectSchemaDefinition

Not much better, but you can keep it DRY using:

const otherPropSchema = object({ subProp: string() }),
export const test = object({
  prop: string(),
  otherProp: otherPropSchema as Schema<InferType<typeof otherPropSchema>>,
});

It's a weird issue, most likely due to some limitation in Typescript. I will test again when 4.0 comes out.

types aren't maintained here currently open an Issue on DT please

It is normal to open issues on the main repo instead of DT because nobody checks DT for typing issues.

that doesn't make any sense. I don't maintain the types, opening PR's here also means no one who can fix them sees it, as well as costing me time. I'm not going to keep a bunch of issues with code in another repo open on the off chance someone there reads these.

Maybe it doesn't but it is still the practice right now. There is currently 3500 issues on DT without any classification and DT contains thousands of packages. How is one supposed to find anything on that? I am really sorry to cost you time as I also maintain some smaller open source libraries and I hate when people pick fights, but this I believe a lot of people now use typescript and need good typing to be able to use your awesome library (I use it in multiple projects myself). At least now if someone search for this problem they will know this is an unsolved problem, I don't really mind it being closed.

EDIT: This particular issue might even be relevant to your typescript implementation that I just saw in the draft PR. I am happy to give it a review if you want one.

I just saw in the draft PR. I am happy to give it a review if you want one.

Definately, more eyes on that is helpful and appreciated

Is there an issue of this in DT? This is really bothering me because I cannot infer my schema types, have to manually type out everything, using Omit<> or Pick<> doesn't work for me as well

I would try upgrading and using the new types

@jquense I am on the latest version

The latest version of yup has its own types if you follow the docs for TS config this should work fine

Was this page helpful?
0 / 5 - 0 ratings

Related issues

the-daniel-rothig picture the-daniel-rothig  路  4Comments

laurazenc picture laurazenc  路  3Comments

haddyo picture haddyo  路  3Comments

Simmetopia picture Simmetopia  路  4Comments

klvfn picture klvfn  路  4Comments