英文:
How to make the following typescript definition more strict?
问题
以下是您要翻译的 TypeScript 类型定义部分:
// 类型定义
export enum TableFilterType {
MULTIPLE = 'multiple',
SINGLE = 'single'
}
export type TableBasePrimaryFilter = {
type: TableFilterType;
id: string | number;
label: string;
plural: string;
icon?: string;
unit?: string;
iconClass?: Array<string>;
optionsListWidth?: number;
};
export enum FilterDataLoadOrigin {
CLIENT = 'CLIENT',
SERVER = 'SERVER',
}
type TablePrimaryFilterCommon = {
id: string | number;
label: string;
plural: string;
type: TableFilterType;
};
export type TableMultiplePrimaryFilterClient = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableMultiplePrimaryFilterServer = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableMultiplePrimaryFilter = TableMultiplePrimaryFilterClient | TableMultiplePrimaryFilterServer;
export type TableSinglePrimaryFilterClient = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableSinglePrimaryFilterServer = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableSinglePrimaryFilter = TableSinglePrimaryFilterClient | TableSinglePrimaryFilterServer;
export type TablePrimaryFilter =
| TableMultiplePrimaryFilter
| TableSinglePrimaryFilter
请注意,我已经移除了代码部分以满足您的要求。如果您需要进一步的帮助或有其他问题,请随时告诉我。
英文:
I have the below typescript definitions
// Type definitions
export enum TableFilterType {
MULTIPLE = 'multiple',
SINGLE = 'single'
}
export type TableBasePrimaryFilter = {
type: TableFilterType;
id: string | number;
label: string;
plural: string;
icon?: string;
unit?: string;
iconClass?: Array<string>;
optionsListWidth?: number;
};
export enum FilterDataLoadOrigin {
CLIENT = 'CLIENT',
SERVER = 'SERVER',
}
type TablePrimaryFilterCommon = {
id: string | number;
label: string;
plural: string;
type: TableFilterType;
};
export type TableMultiplePrimaryFilterClient = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableMultiplePrimaryFilterServer = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableMultiplePrimaryFilter = TableMultiplePrimaryFilterClient | TableMultiplePrimaryFilterServer;
export type TableSinglePrimaryFilterClient = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableSinglePrimaryFilterServer = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableSinglePrimaryFilter = TableSinglePrimaryFilterClient | TableSinglePrimaryFilterServer;
export type TablePrimaryFilter =
| TableMultiplePrimaryFilter
| TableSinglePrimaryFilter
Now the following should give an error (because when mode is client, hasMore and total should not be valid props) but it doesn't
// Example
const x: TablePrimaryFilter = {
id: 'level',
label: 'label',
plural: 'plural',
type: TableFilterType.SINGLE,
mode: FilterDataLoadOrigin.CLIENT,
// NOTE: The below lines should throw an error as hasMore and total should be invalid when mode is FilterDataLoadOrigin.CLIENT
hasMore: true,
total: 100,
};
How should I re-define my types to make it more strict?
I have tried different combinations of unions but nothing gives me the desired result
Edit
Adding a new filter type breaks the strictness
export enum TableFilterType {
MULTIPLE = 'multiple',
SINGLE = 'single',
NUMBER = 'number',
}
export type TableNumberPrimaryFilter = TablePrimaryFilterCommon & {
type: TableFilterType.NUMBER
};
export type TablePrimaryFilter =
| TableMultiplePrimaryFilter
| TableSinglePrimaryFilter
| TableNumberPrimaryFilter
The following example should throw error but it doesn't
// Example
const x: TablePrimaryFilter = {
id: 'level',
label: 'label',
plural: 'plural',
type: TableFilterType.NUMBER,
// NOTE: The below lines should throw an error as mode should be invalid when type is FilterDataLoadOrigin.NUMBER
mode: FilterDataLoadOrigin.SERVER,
hasMore: true,
total: 100,
};
答案1
得分: 1
你的代码片段似乎有问题,因为你在TableSinglePrimaryFilterClient
和TableSinglePrimaryFilterServer
中都定义了mode: TypeEnums.CLIENT
,而后者应该包含mode: TypeEnums.SERVER
。
修改这部分确实会得到期望的结果。
英文:
Your code snippet seems wrong because you define mode: TypeEnums.CLIENT
in both TableSinglePrimaryFilterClient
and TableSinglePrimaryFilterServer
when the latter should contain mode: TypeEnums.SERVER
.
Changing this does yield the desired result.
export enum TableFilterType {
MULTIPLE = 'multiple',
SINGLE = 'single'
}
export type TableBasePrimaryFilter = {
type: TableFilterType;
id: string | number;
label: string;
plural: string;
icon?: string;
unit?: string;
iconClass?: Array<string>;
optionsListWidth?: number;
};
export enum FilterDataLoadOrigin {
CLIENT = 'CLIENT',
SERVER = 'SERVER',
}
type TablePrimaryFilterCommon = {
id: string | number;
label: string;
plural: string;
type: TableFilterType;
};
export type TableMultiplePrimaryFilterClient = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableMultiplePrimaryFilterServer = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableMultiplePrimaryFilter = TableMultiplePrimaryFilterClient | TableMultiplePrimaryFilterServer;
export type TableSinglePrimaryFilterClient = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableSinglePrimaryFilterServer = TablePrimaryFilterCommon & {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableSinglePrimaryFilter = TableSinglePrimaryFilterClient | TableSinglePrimaryFilterServer;
export type TablePrimaryFilter =
| TableMultiplePrimaryFilter
| TableSinglePrimaryFilter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论