How to correctly use mapped types

Oleg Gulevskyy

I struggle to define a good types for my use case. Here is what I have: One global object that represents Settings

const settings = {}

This object contains each individual menu mapped Menu title -> Menu settings.

const settings = {
  menu1: {
    showOneThing: true,
    showAnotherThing: false,
  },
  menu2: {
    renderThis: true,
    renderThat: false,
  }
}

I am trying to come up with a function that would return different properties available in the menu, based on what menu is selected. For example

const getSettings = (menuKey) => ...
const settingsForMenu1 = getSettings('menu1');
settingsForMenu1.showOneThing // intellisense and all of that
const settingsForMenu2 = getSettings('menu2');
settingsForMenu2.renderThis // property renderThis or renderThat are the only ones that should be available here

Now for some reason I seem to struggle with concept of generics. I think I need to use the concept of mapped types here, but I really can't figure out how. Would anyone be able to suggest ways to do what I am trying to do?

Thanks

Tobias S.

Would this work for you?

function getSettings<
  K extends keyof typeof settings
>(key: K): (typeof settings)[K] {
  return settings[key]
}

Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript - How to use nested mapped types

How to use Mapped Types with React.useState?

How to use MapTo Correctly With Sum Types

How to correctly use types with FormArray in Angular

How to correctly use types in universal function?

How to use the original map key when using the Mapped Types with the Template Literal Types?

How to use the mapped projection * <>

Typescript type inference with mapped types does not work correctly

How to correctly use size_t and other integer types together?

How do I use arrays of generic types correctly?

How do I use mapped types to ensure all nested children are represented?

Is it possible to use mapped types in Typescript to change a types key names?

How to replace properties using mapped types in Typescript

How can preserve the types for a mapped object in Typescript

How to correctly truncate integral types

TypeScript Mapped Types - Is there a way to use interface value type as parameter of function type in mapped type?

RestKit response not mapped correctly

TypeScript how to use mapped type in module declaration?

How To Use Memory Mapped File in Python Linux

How to use prefect "mapped" to parallelize nested loops

How to use `Mapped[]` and `with_variant` in SQLAlquemy 2?

How to Use std::any as mapped_type

How to use an api that is mapped to a service in Kubernetes

How use instead of comma in mapped list

How to use a Mock in xunit passing a mapped entity

How do I correctly use multiple instances of the infer keyword with template literal types

how to use it correctly useSelector()

How to correctly use Realm

How to use MonadError correctly?