-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[charts] Provide context-aware types based on props #14340
Comments
Other options could be interface Props<IsDataset> {
dataset: IsDataset ? object[] : undefined
series: BarSeries<IsDataset>
} or interface PropsWithDataset extends CommonProps { ... }
interface PropsWithoutDataset extends CommonProps { ... }
function Component (props: PropsWithDataset)
function Component (props: PropsWithoutDataset)
function Component (props: PropsWithoutDataset | PropsWithtDataset) |
the generic interface approach doesn't seem to work because the generic needs to be defined, and can't be automatically inferred by the passed prop interface Gen<IsDataset> {
dataset?: IsDataset extends any[] ? string[] : never;
series?: IsDataset extends any[] ? never : string[];
}
const ComponentGen = <IsDataset extends any[]>(props: Gen<IsDataset>) => {
return null;
};
export default function Page() {
return (
<>
<ComponentGen dataset={['aaa']} />
<ComponentGen series={['aaa']} />
<ComponentGen dataset={['aaa']} series={['aaa']} />
</>
);
} The second approach can work nicely. It also picks up info from the input props if necessary, eg: interface Prop1<Dataset extends any[] = any[]> {
height: number;
width: number;
data: Dataset;
dataKey: Dataset extends (infer DataItem)[] ? keyof DataItem : never;
responsive?: never;
};
interface Prop2 {
responsive: true;
data: string[];
};
const Component = <Dataset extends any[]>(props: Prop1<Dataset> | Prop2) => {
return null;
};
export default function Page() {
return (
<>
<Component responsive data={['aaa']} />
<Component height={1} width={2} data={[{ go: true, nice: 'foo' }]} dataKey="go" />
{/* Unexpected prop value */}
<Component responsive={false} />
{/* Data should be string when used with responsive */}
<Component responsive data={[1]} />
</>
);
} |
WHile working on the typing, I noticed that because we extends configuration, the distinction between pro and community props is mixed. For example If you have in your project a mix of pro and community component, the axis config will be typed as a pro one. <BarChartPro xAxis={[{ zoom: true }]} /> // TS passes and it's expected
<BarChart xAxis={[{ zoom: true }]} /> // TS passes but should fail because MIT chart don't have the zoom |
True, the |
A current frustration with types right now is that we can't provide context aware types for the users due to how our types are declared.
This change could provide valuable IDE information to our final users in how the components can be used.
Example
Possible implementations
dataset
vsno dataset
dataset
mode could require that thedataKey
is set in the seriesdata
mode would forbiddataKey
and requiredata
Search keywords:
The text was updated successfully, but these errors were encountered: