#Shapeshift nested object

1 messages ยท Page 1 of 1 (latest)

opal walrus
#

How can I use @sapphire/shapeshift with nested objects and TypeScript compile. The types are known in VS-Code, but when compiled, the nested objects are turned into any...

const autoCompleteSubreddit = s.object({
    numSubscribers: s.number,
    name: s.string,
    allowedPostTypes: s.object({
        images: s.boolean,
        text: s.boolean,
        videos: s.boolean,
        links: s.boolean,
        spoilers: s.boolean,
    }),
    id: s.string,
    primaryColor: s.string,
    communityIcon: s.union(s.literal(""), s.string.url()),
    icon: s.union(s.literal(""), s.string.url()),

});

export type SubredditAutoComplete = InferType<typeof autoCompleteSubreddit>[];

turnes into (.d.ts)

declare const autoCompleteSubreddit: import("@sapphire/shapeshift").ObjectValidator<{
    numSubscribers: number;
    name: string;
    allowedPostTypes: import("@sapphire/shapeshift").UndefinedToOptional<{
        images: any;
        text: any;
        videos: any;
        links: any;
        spoilers: any;
    }>;
    id: string;
    primaryColor: string;
    communityIcon: string;
    icon: string;
}, import("@sapphire/shapeshift").UndefinedToOptional<{
    numSubscribers: number;
    name: string;
    allowedPostTypes: import("@sapphire/shapeshift").UndefinedToOptional<{
        images: any;
        text: any;
        videos: any;
        links: any;
        spoilers: any;
    }>;
    id: string;
    primaryColor: string;
    communityIcon: string;
    icon: string;
}>>;
export type SubredditAutoComplete = InferType<typeof autoCompleteSubreddit>[];
scenic nicheBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> โœ… Mark Solution

desert remnant
#

@digital bear

digital bear
#

Well, tsconfig?

#

I'm not available until late tonight, but I'll most likely be dead tired by then

opal walrus
#

i haven't looked at it in a while (did the config ages ago)

desert remnant
#

Good lord, that's a setup and a half

  • if true: conflicts with discord-api-types untrue, @sapphire/ts-config/extra-strict also sets it to true and it works just fine
  • your jsconfig and tsconfig diverge. You should remove jsconfig and either update or keep tsconfig like it is
  • tsconfig excludes is only to exclude what is included by includes, your excludes is pretty useless
  • you really shouldnt be including your test files in your tsconfig. Either set up a tsconfig for each src and test directory, or dont at all seeing as your tests arent even typescript to begin with.
  • above will also let you set rootDir for src so you dont get dist/src/* but just dist/* which is much cleaner.

at any rate, it's a reproduceable issue

opal walrus
#

thanks for your recommendations

#
  • im getting the result in dist/* not dist/src/*
desert remnant
#

hm

opal walrus
#
  • yeah it is big (i should clean it)
desert remnant
#

probably because of the wildcard src/* instead of src then

#

but point still stands

#

that would probably also mean that if your tests were TS then they'd also end up in dist

opal walrus
#

idk why i was adding my tests to ts... i think it was to let it check for issues with my code (but i have disable checking js...)

desert remnant
#

the error as referring to the any's? yes as I said, it's reproducible. Idk why that happens.

opal walrus
#

ye

#

but thanks for your recomendations ๐Ÿ‘

opal walrus
desert remnant
#

that's on how you configure your eslint parser.

opal walrus
#

its just "parser": "@typescript-eslint/parser", basicly

desert remnant
opal walrus
#

oh oh, ill look in to that

#

but yeah, glad to have brought up the issue (hope the team can fix it sometime ๐Ÿ™‚ )

desert remnant
#

@opal walrus so I tried a thing that I thought might be related but it's not. I really have no idea why TS does this. It's extremely black box too really. My best guess is that TS fallsback to any to prevent a runaway type instantiation but I have no idea how we could fix this.

I have 2 recommendations

  1. Create an issue on the typescript repo. Very highly recommended. For example a good issue that ended up involving shapeshift is https://github.com/microsoft/TypeScript/issues/54491. Hopefully someone on the TS team can guide both you why it's any and what we can do to ensure it is no longer any.

  2. Using SchemaOf ensures that you don't get any's:

import { s, type SchemaOf } from '@sapphire/shapeshift';

interface SubredditAllowedPostTypes {
    images: boolean;
    text: boolean;
    videos: boolean;
    links: boolean;
    spoilers: boolean;
}

interface SubredditAutoComplete {
    numSubscribers: number;
    name: string;
    allowedPostTypes: SubredditAllowedPostTypes;
    id: string;
    primaryColor: string;
    communityIcon: string;
    icon: string;
}

export const allowedPostTypes: SchemaOf<SubredditAllowedPostTypes> = s.object({
    images: s.boolean,
    text: s.boolean,
    videos: s.boolean,
    links: s.boolean,
    spoilers: s.boolean
});

export const autoCompleteSubreddit: SchemaOf<SubredditAutoComplete> = s.object({
    numSubscribers: s.number,
    name: s.string,
    allowedPostTypes,
    id: s.string,
    primaryColor: s.string,
    communityIcon: s.union(s.literal(''), s.string.url()),
    icon: s.union(s.literal(''), s.string.url())
});

This gives .d.ts of:

import { type SchemaOf } from '@sapphire/shapeshift';
interface SubredditAllowedPostTypes {
    images: boolean;
    text: boolean;
    videos: boolean;
    links: boolean;
    spoilers: boolean;
}
interface SubredditAutoComplete {
    numSubscribers: number;
    name: string;
    allowedPostTypes: SubredditAllowedPostTypes;
    id: string;
    primaryColor: string;
    communityIcon: string;
    icon: string;
}
export declare const allowedPostTypes: SchemaOf<SubredditAllowedPostTypes>;
export declare const autoCompleteSubreddit: SchemaOf<SubredditAutoComplete>;
export {};
//# sourceMappingURL=test-type.d.ts.map

So using SchemaOf you go from interface to validation schema, rather than from validation schema to interface like you would with InferType

opal walrus
#

Ah, thank you for that recommendation, I think I will do the second one as I don't really know how to make a good issue... (And do not have much time right now)