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

TS misunderstands types #31

Closed
babakfp opened this issue Mar 2, 2024 · 2 comments
Closed

TS misunderstands types #31

babakfp opened this issue Mar 2, 2024 · 2 comments

Comments

@babakfp
Copy link

babakfp commented Mar 2, 2024

Hi

export type Config = {
    presets?: {
        /** @default false */
        flexGrid?: boolean
        /** @default false */
        fontWeightRegular?: boolean
        /** @default true */
        moreDefaultValues?: boolean
        /** @default false */
        screenToDynamicScreen?: boolean
    }
    utilities?: {
        /** @default true */
        bgGrid?: boolean
        /** @default true */
        bgRadial?: boolean
        /** @default true */
        dir?: boolean
        /** @default true */
        drag?: boolean
        /** @default true */
        flip?: boolean
        /** @default true */
        hideShow?: boolean
        /** @default true */
        inputResets?: boolean
        /** @default true */
        insetCenter?: boolean
        /** @default true */
        overflowUnset?: boolean
        /** @default true */
        tapHighlight?: boolean
    }
    variants?: {
        /** @default true */
        notVariants?: boolean
    }
}
const defaultConfig = {
    presets: {
        flexGrid: false,
        fontWeightRegular: false,
        moreDefaultValues: true,
        screenToDynamicScreen: false,
    },
    utilities: {
        bgGrid: true,
        bgRadial: true,
        dir: true,
        drag: true,
        flip: true,
        hideShow: true,
        inputResets: true,
        insetCenter: true,
        overflowUnset: true,
        tapHighlight: true,
    },
    variants: {
        notVariants: true,
    },
} satisfies Config
export default (userConfig?: Config) => {
    const config = merge(defaultConfig, userConfig || {})
}

The issue is, TS shows me this error, for example, fontWeightRegular in config.presets?.fontWeightRegular can be undefined.

It's expected that fontWeightRegular will always have a value. But TS doesn't know that.

I want to merge the default options with the same options (with custom values) added by the user.

@voodoocreation
Copy link
Owner

voodoocreation commented Mar 3, 2024

Hey! This appears to be a duplicate of issues #28 and #30 - it's a known gap in the type inferring that's provided within this package, but I haven't been able to rewrite it in a way where TS is able to be more aware of what the value will be based on the order of events within the unlimited objects and recursion that's going on within the runtime code, which is why it can results in a union of all possible values (which will include undefined if that's a possible value with what's being passed in). Though your declared Config type won't be helping things, as you've marked everything as being optional, which will result in everything being possibly undefined. A lot of the type inferring that this package does won't be doing too much due to your example working with declared types rather than inferring based on the runtime declaration.

What I would suggest is to declare your Config type with all properties that you know will always be defined without marking them as optional with ?, then for your function parameter, assign the parameter userConfig?: Partial<Config> (or DeepPartial if using utility-types), then you can just assign the return type as Config which is more accurate for what you're trying to achieve.

More context is available in issues #28 and #30 though, and PRs are always welcome if you'd like to try and improve the type inferring.

@babakfp
Copy link
Author

babakfp commented Mar 3, 2024

Thanks.

@babakfp babakfp closed this as completed Mar 3, 2024
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