Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
Typescript Passing prop as one of the objects's keys - values

I am using react native and trying to pass one of string values to another component.

The type object looks like this:

export const ScannerAction = {
  move: 'move',
  inventory: 'inventory',
  demand: 'demand',
  supply: 'supply'
};

so when I pass a value called operationType I want it to be one of the strings: move, inventory, demand or supply.

the child component would have an interface like this:

interface IIProps {
  id: string;
  otherStuff: any;
  operationType: should be the type of ScannerAction
}

I could use

operationType: 'supply' | 'demand' | 'inventory' | 'move'

but I want it to be dynamic and editable only in one place. What should I do?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The best way would be to use an enum instead of an object:

enum ScannerAction {
  move = 'move',
  inventory = 'inventory',
  demand = 'demand',
  supply = 'supply'
};

interface IIProps {
  id: string;
  otherStuff: any;
  operationType: ScannerAction
}

If you want to stick to an object, you can use keyof to get the keys and then get the values:

const ScannerAction = {
  move: 'move',
  inventory: 'inventory',
  demand: 'demand',
  supply: 'supply'
} as const; // make this const so that ScannerActionValues becomes a union of all values instead of string

type ScannerActionValues = typeof ScannerAction[keyof typeof ScannerAction];

interface IIProps {
  id: string;
  otherStuff: any;
  operationType: ScannerActionValues;
}
about 4 years ago · Juan Pablo Isaza Report

0

What about to use enum? https://www.typescriptlang.org/docs/handbook/enums.html

export enum ScannerAction {
    move = 'move',
    inventory = 'inventory',
    demand = 'demand',
    supply = 'supply'
}

interface usage:

interface IIProps {
    id: string;
    otherStuff: any;
    operationType: ScannerAction
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!