如何使以下 TypeScript 定义更严格?

huangapple go评论56阅读模式
英文:

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 = &#39;multiple&#39;,
SINGLE = &#39;single&#39;
}
export type TableBasePrimaryFilter = {
type: TableFilterType;
id: string | number;
label: string;
plural: string;
icon?: string;
unit?: string;
iconClass?: Array&lt;string&gt;;
optionsListWidth?: number;
};
export enum FilterDataLoadOrigin {
CLIENT = &#39;CLIENT&#39;,
SERVER = &#39;SERVER&#39;,
}
type TablePrimaryFilterCommon = {
id: string | number;
label: string;
plural: string;
type: TableFilterType;
};
export type TableMultiplePrimaryFilterClient = TablePrimaryFilterCommon &amp; {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableMultiplePrimaryFilterServer = TablePrimaryFilterCommon &amp; {
mode: FilterDataLoadOrigin.SERVER;
hasMore: boolean;
total: number;
};
export type TableMultiplePrimaryFilter = TableMultiplePrimaryFilterClient | TableMultiplePrimaryFilterServer;
export type TableSinglePrimaryFilterClient = TablePrimaryFilterCommon &amp; {
mode: FilterDataLoadOrigin.CLIENT;
};
export type TableSinglePrimaryFilterServer = TablePrimaryFilterCommon &amp; {
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: &#39;level&#39;,
label: &#39;label&#39;,
plural: &#39;plural&#39;,
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 = &#39;multiple&#39;,
SINGLE = &#39;single&#39;,
NUMBER = &#39;number&#39;,
}
export type TableNumberPrimaryFilter = TablePrimaryFilterCommon &amp; {
type: TableFilterType.NUMBER
};
export type TablePrimaryFilter =
| TableMultiplePrimaryFilter
| TableSinglePrimaryFilter
| TableNumberPrimaryFilter

The following example should throw error but it doesn't

// Example
const x: TablePrimaryFilter = {
id: &#39;level&#39;,
label: &#39;label&#39;,
plural: &#39;plural&#39;,
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

你的代码片段似乎有问题,因为你在TableSinglePrimaryFilterClientTableSinglePrimaryFilterServer中都定义了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 = &#39;multiple&#39;,
  SINGLE = &#39;single&#39;
}

export type TableBasePrimaryFilter = {
  type: TableFilterType;
  id: string | number;
  label: string;
  plural: string;
  icon?: string;
  unit?: string;
  iconClass?: Array&lt;string&gt;;
  optionsListWidth?: number;
};

export enum FilterDataLoadOrigin {
  CLIENT = &#39;CLIENT&#39;,
  SERVER = &#39;SERVER&#39;,
}

type TablePrimaryFilterCommon = {
  id: string | number;
  label: string;
  plural: string;
  type: TableFilterType;
};

export type TableMultiplePrimaryFilterClient = TablePrimaryFilterCommon &amp; {
  mode: FilterDataLoadOrigin.CLIENT;
};

export type TableMultiplePrimaryFilterServer = TablePrimaryFilterCommon &amp; {
  mode: FilterDataLoadOrigin.SERVER;
  hasMore: boolean;
  total: number;
};

export type TableMultiplePrimaryFilter = TableMultiplePrimaryFilterClient | TableMultiplePrimaryFilterServer;

export type TableSinglePrimaryFilterClient = TablePrimaryFilterCommon &amp; {
  mode: FilterDataLoadOrigin.CLIENT;
};

export type TableSinglePrimaryFilterServer = TablePrimaryFilterCommon &amp; {
  mode: FilterDataLoadOrigin.SERVER;
  hasMore: boolean;
  total: number;
};

export type TableSinglePrimaryFilter = TableSinglePrimaryFilterClient | TableSinglePrimaryFilterServer;

export type TablePrimaryFilter =
  | TableMultiplePrimaryFilter
  | TableSinglePrimaryFilter

huangapple
  • 本文由 发表于 2023年5月15日 01:07:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定