Coder Social home page Coder Social logo

Comments (7)

bennettdams avatar bennettdams commented on May 18, 2024 3

Sorry for bringing this up again, but maybe this can be fixed by using conditional types & overloads based on the props? I've never used conditional types/overloads together with class components, so not sure if this would work at all...

I created an oversimplified example, which would result in the following (screenshot). Depending on the props, you would get type-safe access to the height/width property based on whatever has been disabled via props. Look at the expected errors:

image


Here's the example code:

Playground link

type PropsConfig = {
    disableWidth?: boolean
    disableHeight?: boolean
}

type Props = {
    otherProps?: string
} & PropsConfig

type Size = {
    width: number
    height: number
}

type AutoSizerReturn<TSize extends Partial<Size>> = () => TSize

type SizeConditional<TKeyToOmit extends keyof Size> = Omit<Size, TKeyToOmit>

// simulating a component..
function autoSizer<TProps extends Props>(props: TProps):
    TProps["disableWidth"] extends true
    ? (TProps["disableHeight"] extends true
        ? AutoSizerReturn<SizeConditional<"width" | "height">>
        : AutoSizerReturn<SizeConditional<"width">>)
    : TProps["disableHeight"] extends true
    ? AutoSizerReturn<SizeConditional<"height">>
    : AutoSizerReturn<Size> {

    const size: Size = {
        width: 123,
        height: 123
    }

    // simulating render props..
    return () => size
}

// Example 1: nothing disabled, both height & width are there
const size1 = autoSizer({})()
const size1Height = size1.height
const size1Width = size1.width

// Example 2: disable height, only width is there
const size2 = autoSizer({ disableHeight: true })()
const size2Height = size2.height
const size2Width = size2.width

// Example 3: disable width, only height is there
const size3 = autoSizer({ disableWidth: true })()
const size3Height = size3.height
const size3Width = size3.width

// Example 4: disable both, none are there
const size4 = autoSizer({ disableHeight: true, disableWidth: true })()
const size4Height = size4.height
const size4Width = size4.width

from react-virtualized-auto-sizer.

bvaughn avatar bvaughn commented on May 18, 2024 3

@bennettdams That does seem like a nice DevX improvement. Thanks for the playground link.

I'd be happy to review a PR for this. Else I'll try to get around to it at some point.

from react-virtualized-auto-sizer.

bvaughn avatar bvaughn commented on May 18, 2024 2

In an effort to minimize churn, here's what I've settled on: TypeScript playground link

from react-virtualized-auto-sizer.

shhider avatar shhider commented on May 18, 2024 1

+1, it caused typechecking problems

from react-virtualized-auto-sizer.

Vader19695 avatar Vader19695 commented on May 18, 2024

Also seeing a problem with this change. I would need to modify a lot of my codebase to handle this change. Heads up that reverting to v1.0.7 seems to fix the issue.

from react-virtualized-auto-sizer.

bvaughn avatar bvaughn commented on May 18, 2024

I'm sorry this changed cause type issues for you, but the new Size type is more accurate type than was there prior, as both width and height params are conditional based on the disableWidth and disableHeight props.

from react-virtualized-auto-sizer.

bvaughn avatar bvaughn commented on May 18, 2024

1.0.18

from react-virtualized-auto-sizer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.