Comments (7)
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:
Here's the example code:
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.
@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.
In an effort to minimize churn, here's what I've settled on: TypeScript playground link
from react-virtualized-auto-sizer.
+1, it caused typechecking problems
from react-virtualized-auto-sizer.
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.
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.
1.0.18
from react-virtualized-auto-sizer.
Related Issues (20)
- Missing class properties transform HOT 3
- The container's width (or the resize event) does not update if the parent has a scaling animation HOT 10
- The AutoSizer does not have correct width in a popover. HOT 1
- AutoSizer height and width changed from type `number` to `number | undefined` in 1.0.8 HOT 5
- `Module parse failed: Unexpected token` error HOT 3
- ResizeObserver loop limit exceeded HOT 6
- AutoSizer cannot be used as a JSX component typescript error HOT 4
- Reporting an integer size when the available space has decimals HOT 6
- "react-virtualized-auto-sizer 新版本重复渲染子组件,和 antd tabs 冲突 HOT 2
- API change in return type HOT 2
- Args is type `any` in latest version (1.0.18) HOT 23
- "Can't perform a React state update on an unmounted component" console error HOT 5
- 1.0.19 does not render anything HOT 1
- Is it possible to test AutoSizer-powered components in unit tests? HOT 8
- Types of disableHeight and disableWidth are incorrect HOT 6
- TypeScript: Incorrect typing of AutoSize height and width HOT 2
- Typings for conditional `disableHeight` and `disableWidth` HOT 2
- 'AutoSizer' cannot be used as a JSX component. HOT 1
- Is it possible to render content inside auto sizer callback on server? HOT 1
- AutoSizer not rendering initially with consistent height for fixed height components HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-virtualized-auto-sizer.