Typescript custom key mapping

  Kiến thức lập trình
interface IObj {
    value:string;
    value2:string;
    value3:number;
}
let obj={
    value:"JS",
    value2:"www.google.com",
    value3:2018
}
type kM = "name"|"website"|"year";
const keyMap :{[key in kM]:keyof IObj}= {
    name:"value",
    website:"value2",
    year:"value3"
}
const year:number  = obj[keyMap["year"]]; // ERROR: Type 'string | number' is not assignable to type 'number'. Type 'string' is not assignable to type 'number'.
obj[keyMap["name"]] = "Javascript";  // ERROR: Type 'string' is not assignable to type 'never'.

I attempted to customize the mapping or access the key within an object, but encountered an error.
What steps should be taken to address the error stated above?

LEAVE A COMMENT