• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

133
Views
Using union type as function parameter

I have a type defined like so:

type FieldToAction = {
            [key in ApiActions]: ((field: Field) => void) | ((field: Field, fieldValue: FieldValue) => void)
        }

where FieldValue is a union of types:

type FieldValue = string | string[] | AssigneeValue | PriorityValue

I have a variable object where I then declare the functions set by the above types:

const fieldToAction: FieldToAction = {
            .
            .
            .
            "SET_DESCRIPTION": (field: Field, fieldValue: string) => field.setDescription(fieldValue),
            "SET_LABELS_VALUE": (field: Field, fieldValue: string[]) => field.setValue(fieldValue)
        };

This produces an error where the values in the object are not of type FieldToAction, I kind of understand why since I am now constraining the parameter to be a string or string[].

My question is: Is there a way of still using the union of types' type and constraining the value in the parameter?

Thank you

almost 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error