Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  194] [ 7]  / answers: 1 / hits: 78510  / 5 Years ago, tue, november 19, 2019, 12:00:00

Here's the code:





export type Period = 'dy' | 'wk' | 'mn' | 'qt' | 'yr';

const periods: Record<Period, string> = {
dy: 'Day',
wk: 'Week',
mn: 'Month',
qt: 'Quarter',
yr: 'Year'
};





When I try to do this:



const key = Object.keys(periods).find(key => periods[key] === 'Day');



I get an error of course, since periods[key] cannot guarantee that key is of the correct type. How should I really go about doing this? I thought of an enum but I can't do reverse lookups. All I'm trying to achieve is an input field that displays 'Day' but has a key of dy (etc.) and can set the state to the correct key and not the value when the user picks another value.


More From » reactjs

 Answers
32

Object.keys returns string[] not Array<keyof T> (where T is the type of the value passed in). The reasons for this are outlined here.



Since your object is probably not going to have unknown keys, you can use a type assertion:



export type Period = 'dy' | 'wk' | 'mn' | 'qt' | 'yr';

const periods: Record<Period, string> = {
dy: 'Day',
wk: 'Week',
mn: 'Month',
qt: 'Quarter',
yr: 'Year'
};

const key = (Object.keys(periods) as Array<Period>).find(key => periods[key] === 'Day');


Playground Link


[#51466] Saturday, November 9, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elians

Total Points: 417
Total Questions: 87
Total Answers: 101

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
;