-
-
Notifications
You must be signed in to change notification settings - Fork 230
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
fix: FunctionKey and NonFunctionKeys #95
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @promer94, thanks for the PR. The current behavior is intentional.
To get non-nullable keys you could use type composition like this:
type OmitOptional<T> = Omit<T, OptionalKeys<T>>;
type Keys = FunctionKeys<OmitOptional<MixedProps>>
I like that you extended tests, and I would gladly merge them but without optional keys change highlighted in the comments.
src/mapped-types.ts
Outdated
* | ||
* // Expect: "setName" | ||
* type Keys = FunctionKeys<MixedProps>; | ||
*/ | ||
export type FunctionKeys<T extends object> = { | ||
[K in keyof T]: T[K] extends Function ? K : never | ||
[K in keyof T]-?: T[K] extends Function ? K : never |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this change, it's not desired.
src/mapped-types.ts
Outdated
}[keyof T]; | ||
|
||
/** | ||
* NonFunctionKeys | ||
* @desc get union type of keys that are non-functions in object type `T` | ||
* @example | ||
* type MixedProps = { name: string; setName: (name: string) => void }; | ||
* type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; | ||
* | ||
* // Expect: "name" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- // Expect: "name" | "someKeys"
src/mapped-types.ts
Outdated
@@ -89,26 +89,30 @@ export type NonUndefined<A> = A extends undefined ? never : A; | |||
* FunctionKeys | |||
* @desc get union type of keys that are functions in object type `T` | |||
* @example | |||
* type MixedProps = { name: string; setName: (name: string) => void }; | |||
* type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; | |||
* | |||
* // Expect: "setName" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Expect: "setName" | "someFn"
Thanks for your feedback 😀. type FunctionKeys<T extends object> = {
[K in keyof T]: T[K] extends Function ? K : never
}[keyof T]
type MixedProps = {
name: string;
setName: (name: string) => void;
someKeys?: string;
someFn?: (...args: any) => any;
};
// Expect: "setName" | undefined
type Keys = FunctionKeys<MixedProps> if i don't change the implementation, the type of Keys would be 'setName' | undefined rather than 'setName' | 'someFn'. I'm new to Typescript, let me know if i am wrong |
@promer94 Ok I see now, it seems the type is broken, it definitely shouldn't include undefined. This should work
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks sweet! Great job @promer94, thanks.
#41 Description
FunctionKeys and NonFunctionKeys cant work with optional keys
example
Checklist
For bugfixes: