Skip to content
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

feat: add typeParts function #374

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/types/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export function isFalsyType(type: ts.Type): boolean {
}

/**
* Get the union type parts of the given type.
* Get the intersection type parts of the given type.
*
* If the given type is not a union type, an array contain only that type will be returned.
* If the given type is not a intersection type, an array contain only that type will be returned.
* @category Types - Utilities
* @example
* ```ts
Expand All @@ -71,6 +71,26 @@ export function intersectionTypeParts(type: ts.Type): ts.Type[] {
return isIntersectionType(type) ? type.types : [type];
}

/**
* Get the intersection or union type parts of the given type.
*
* Note that this is a shallow collection: it only returns `type.types` or `[type]`.
*
* If the given type is not an intersection or union type, an array contain only that type will be returned.
* @category Types - Utilities
* @example
* ```ts
* declare const type: ts.Type;
*
* for (const typePart of intersectionTypeParts(type)) {
* // ...
* }
* ```
*/
export function typeParts(type: ts.Type): ts.Type[] {
return isIntersectionType(type) || isUnionType(type) ? type.types : [type];
}

function isReadonlyPropertyIntersection(
type: ts.Type,
name: ts.__String,
Expand Down
Loading