50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
|
|
export interface Base {
|
|
id: number
|
|
createdAt: string | null
|
|
updatedAt: string | null
|
|
deletedAt?: string | null
|
|
}
|
|
|
|
export interface CodeName {
|
|
name: string
|
|
code: string
|
|
}
|
|
|
|
export interface TreeItem {
|
|
value: string
|
|
label: string
|
|
hasChildren: boolean
|
|
children?: TreeItem[]
|
|
}
|
|
|
|
export function genBase(): Base {
|
|
return {
|
|
id: 0,
|
|
createdAt: '',
|
|
updatedAt: '',
|
|
}
|
|
}
|
|
|
|
export type WithBase<T extends Base> = Omit<T, keyof Base> & Required<Base>
|
|
|
|
/**
|
|
* The function `withBase` sets default values for item properties if not provided.
|
|
* @param item - The `withBase` function takes in an optional parameter `item`, which is a
|
|
* partial object of type `T`. The function merges the properties of `item` with default values for
|
|
* `id`, `createdAt`, `updatedAt`, and `deletedAt`. The function then returns an object of type
|
|
* @returns The `withBase` function returns an object with default values for the properties
|
|
* `id`, `createdAt`, `updatedAt`, and `deletedAt`, along with any additional properties provided in
|
|
* the `item` parameter. The returned object has a type `WithBase<T>`, which extends `T` and
|
|
* includes the default properties.
|
|
*/
|
|
export function withBase<T extends Base>(item: Partial<T> = {}): WithBase<T> {
|
|
return {
|
|
id: 0,
|
|
createdAt: null,
|
|
updatedAt: null,
|
|
deletedAt: null,
|
|
...item,
|
|
} as WithBase<T>
|
|
}
|