How can I generate a type of string literals from an objects keys and values returned by a function?

GuyC

I am trying to create a type from a readonly object, i.e.

const Actions = {

  'user.crud': ['user.create', 'user.read', 'user.update', 'user.delete'],

} as const

type ActionsType = keyof typeof Actions | typeof Actions[keyof typeof Actions][number]

The above works nicely and sets up the type ActionsType to the string literals ('user.crud', 'user.create', etc..).

However, the Actions object above is very simplistic, instead, I really need to generate the Actions via functions. When I port the above over to being generated by a function, i.e.

// set up a function to generate all actions for the passed role
function getActions (role: string): Record<string, string[]> {

    return {
        [`${role}.crud`]: [`${role}.create`, `${role}.read`, `${role}.update`, `${role}.delete`],
    }

}

// generate the Actions from a function
const ActionsFromFunction = {

  ...getActions('user'),

} as const

// set up the Actions from a readonly object with values generated by getActions()
type ActionsFromFunctionType = keyof typeof ActionsFromFunction | typeof ActionsFromFunction[keyof typeof ActionsFromFunction][number]

the type ActionsFromFunctionType is no longer set to the string literals. Instead it is set to: string | number and in turn type tests fail as any string is accepted.

I've put together a demo of the above:

Playground

Is there a way of generating the Actions object via a function, whilst still maintaining the string literals within the type?

Temoncher

Your goal is only achievable through typescript Template literal types. They are not supported in typescript 4.0, but will be available in 4.1 version.

This is how you could do it with typescript 4.1

type CrudOperations<ROLE extends string> = [`${ROLE}.create`, `${ROLE}.read`, `${ROLE}.update`, `${ROLE}.delete`];

type GetActionsResult<ROLE extends string> = string extends ROLE // check if we can infer type
  ? { [k: string]: string[] } // if type is not inferable
  : { [K in `${ROLE}.crud`]: CrudOperations<ROLE> };

function getActions<ROLE extends string>(role: ROLE): GetActionsResult<ROLE> {
    return {
      [`${role}.crud`]: [`${role}.create`, `${role}.read`, `${role}.update`, `${role}.delete`]
    } as GetActionsResult<ROLE>;
}

// falls back to { string: string[] } structure
const actions = getActions('admin' as string);

// generate the Actions from a function
const ActionsFromFunction = {
  ...getActions('user'),
  ...getActions('orders'),
}

Playground link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I remove some keys from a JSON string where the values are preventing me from parsing it as JSON?

From Dev

How can I detect string literals in code?

From Java

How can I ignore certain returned values from array destructuring?

From Dev

How can I convert object keys & values to formatted string?

From Dev

How can I randomly generate one of two values for a string?

From Dev

How can I read keys and values from nestable list?

From Dev

How can I get keys from list of values?

From Dev

How can I generate weak SSH keys?

From Dev

How can I print values of nested objects of JSON string?

From Dev

How can I change the color of string values I type?

From Dev

How can I stop Python from recognizing string literals such as "\n" or "\b"?

From Dev

How can I convert a unicode string into string literals in Python 2.7?

From Dev

How can I convert a unicode string into string literals in Python 2.7?

From Dev

how to Split the String values returned from the method

From Dev

Java: How can I generate PrivateKey from a string?

From Dev

How can I generate a barcode from a string in Swift?

From Dev

How can I use objects as keys in a JSON?

From Dev

Why can't I access properties of an anonymous type returned from a function via the dynamic keyword?

From Dev

How to use a string array returned from a function?

From Dev

How do I type a function with input and output objects with the same keys but different value types?

From Dev

How can I match the array values (e.g values from i++) to the keys in arrays

From Dev

How can I select a subset of values from an array using the values from another array as keys?

From Dev

How do I compare the String returned from a function to another String variable in Javascript

From Dev

How to append string literals to properties returned from a Select-Object command

From Dev

How can I type in flow a high order function that takes a function to generate data?

From Dev

How to get values from string by matching keys?

From Dev

How do I dereference the address returned by a pointer from a function that takes an int & a pointer of type struct?

From Dev

How to access a subset of values from the returned set of values of a function in Python?

From Dev

Using PHP, how can I access the protected _values property returned from the Stripe API?

Related Related

  1. 1

    How can I remove some keys from a JSON string where the values are preventing me from parsing it as JSON?

  2. 2

    How can I detect string literals in code?

  3. 3

    How can I ignore certain returned values from array destructuring?

  4. 4

    How can I convert object keys & values to formatted string?

  5. 5

    How can I randomly generate one of two values for a string?

  6. 6

    How can I read keys and values from nestable list?

  7. 7

    How can I get keys from list of values?

  8. 8

    How can I generate weak SSH keys?

  9. 9

    How can I print values of nested objects of JSON string?

  10. 10

    How can I change the color of string values I type?

  11. 11

    How can I stop Python from recognizing string literals such as "\n" or "\b"?

  12. 12

    How can I convert a unicode string into string literals in Python 2.7?

  13. 13

    How can I convert a unicode string into string literals in Python 2.7?

  14. 14

    how to Split the String values returned from the method

  15. 15

    Java: How can I generate PrivateKey from a string?

  16. 16

    How can I generate a barcode from a string in Swift?

  17. 17

    How can I use objects as keys in a JSON?

  18. 18

    Why can't I access properties of an anonymous type returned from a function via the dynamic keyword?

  19. 19

    How to use a string array returned from a function?

  20. 20

    How do I type a function with input and output objects with the same keys but different value types?

  21. 21

    How can I match the array values (e.g values from i++) to the keys in arrays

  22. 22

    How can I select a subset of values from an array using the values from another array as keys?

  23. 23

    How do I compare the String returned from a function to another String variable in Javascript

  24. 24

    How to append string literals to properties returned from a Select-Object command

  25. 25

    How can I type in flow a high order function that takes a function to generate data?

  26. 26

    How to get values from string by matching keys?

  27. 27

    How do I dereference the address returned by a pointer from a function that takes an int & a pointer of type struct?

  28. 28

    How to access a subset of values from the returned set of values of a function in Python?

  29. 29

    Using PHP, how can I access the protected _values property returned from the Stripe API?

HotTag

Archive