Skip to content

Commit

Permalink
feat(types): 新增加requiredKeys用于声明对象中的某个字段为必选
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Sep 3, 2024
1 parent f816244 commit fc18f97
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
21 changes: 21 additions & 0 deletions docs/guide/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,24 @@ type KeyType = ObjectKeyOf<Animal>


```


# requiredKeys

用于获取对象 `T` 中指定的属性键 Keys,并将这些键对应的属性设置为必选。

```ts
type Person {
name?: string;
age?: number;
}
// 我们想要创建一个新的类型,其中 name 属性是必选的:
type PersonWithRequiredName = RequiredKeys<Person, 'name'>;

// 这将创建一个新的类型,等同于:
type PersonWithRequiredName = {
name: string;
age?: number;
}

```
12 changes: 6 additions & 6 deletions src/events/flexEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export type FlexListenerRegistry<M,E> = Map<E,FlexEventListenerRegistry<M>>

/**
* Event: 指定一个通用事件类型
*
*
*/
export class FlexEvent<Message=any,Events extends string = string,Options extends Record<string,any>= Record<string,any>>{
// {"<事件名称>":{<listenerId>:[Callback,<侦听次数>]}}
private _listeners:FlexListenerRegistry<Message,Events>= new Map()
private _options:Required<FlexEventOptions & Options>
// 保留最后一次触发的消息,key=事件名称,value=消息
private _lastMessage:Record<string,any> = {}
export class FlexEvent<Message=any,Events extends string = string,Options extends Record<string,any>= Record<string,any>>{
private _listeners:FlexListenerRegistry<Message,Events>= new Map() // {"<事件名称>":{<listenerId>:[Callback,<侦听次数>]}}
private _options:Required<FlexEventOptions & Options>
private _lastMessage:Record<string,any> = {} // 保留最后一次触发的消息,key=事件名称,value=消息
static listenerSeqId:number = 0
constructor(options:FlexEventOptions<Message> = {ignoreError:true,context:null}){
this._options = assignObject({
Expand Down
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export * from "./optional"
export * from "./valueOf"
export * from "./changeReturns"
export * from "./dict"

export * from "./requiredKeys"

// export * from "./typedClassDecorator"
// export * from "./objectPath"
// export * from "./objectPath"
26 changes: 26 additions & 0 deletions src/types/requiredKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


/**
*
* 用于获取对象 T 中指定的属性键 Keys,并将这些键对应的属性设置为必选
*
* @example
* 假设有一个接口 Person {
* name?: string;
* age?: number;
* }
* 我们想要创建一个新的类型,其中 name 属性是必选的:
* type PersonWithRequiredName = RequiredKeys<Person, 'name'>;
* 这将创建一个新的类型,等同于:
* type PersonWithRequiredName = {
* name: string;
* age?: number;
* }
*
* @param T 必须是一个对象类型
* @param keys 必须是 T 的键的联合类型
*
*/
export type RequiredKeys<T extends object, Keys extends keyof T> = T & Required<Pick<T,Keys>>


0 comments on commit fc18f97

Please sign in to comment.