-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Add IsUnion<T> #174
Comments
How do you use it? What problems does it solve? |
export type Field<T extends SemanticType = SemanticType> = {
readonly id: FieldId
readonly type: T
readonly name?: string
readonly description?: string
readonly isHidden?: boolean
}
export const isField = <T extends SemanticType = SemanticType>(
val: unknown,
type?: true extends IsUnion<T> ? T[] : T,
): val is Field<T> => {
if (!isObject(val)) return false
if (!isPlainObject(val)) return false
const field = val as Field
if (!field.id || typeof field.id !== "string") return false
if (!field.type || typeof field.type !== "string") return false
if (Array.isArray(type) && !type.includes(field.type)) return false
if (type && field.type !== type) return false
if (field.name && typeof field.name !== "string") return false
if (field.description && typeof field.description !== "string") return false
if (field.isHidden !== undefined && typeof field.isHidden !== "boolean") {
return false
}
return true
} |
@willium Can you explain? |
I'm restricting the allowed type of Field by the types of value(s) passed in as a second parameter |
Duplicate of #236 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this super helpful in my project, we should add it :)
Upvote & Fund
The text was updated successfully, but these errors were encountered: