Created
April 18, 2025 12:03
-
-
Save nwellis/a401a6915d1518c5142cfb74632bc9d9 to your computer and use it in GitHub Desktop.
Infer type from field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function withType<TElement extends { __type: string }, TType extends TElement["__type"]>( | |
elements: TElement[], | |
type: TType | |
): Extract<TElement, { __type: TType }>[] { | |
return elements?.filter(element => element.__type === type) as Extract<TElement, { __type: TType }>[] | |
} | |
export function withoutType<TElement extends { __type: string }, TType extends TElement["__type"]>( | |
elements: TElement[], | |
type: TType | |
): Exclude<TElement, { __type: TType }>[] { | |
return elements?.filter(element => element.__type !== type) as Exclude<TElement, { __type: TType }>[] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment