I want to construct type {name:string,value:string}
from another type Todo
.
Below code throws error: Property '0' does not exist on type 'Pick<Todo, "items">'.
type Todo = {
title: string;
items:Array<{name:string,value:string}>;
description: string;
completed: boolean;
}
type TodoItems = Pick<Todo, "items">[0]; //ERROR: Property '0' does not exist on type 'Pick<Todo, "items">'.
const todo: TodoItems = {
name: "Clean room",
value: "yes",
};
Note:
Todo
comes from ats
module so it's not editable.
What approach could I follow to extract {name:string,value:string}
from Todo
?
CodePudding user response:
I think you need to split Todo item into separate type as Dmitrii Zolotuhin said, it will be the rightest way. BUT if you can't do it, you can create this type
type TodoItems = Todo["items"][number]
// ...or
type TodoItems = Pick<Todo, "items">["items"][number]
CodePudding user response:
You were almost right, try type TodoItems = Todo["items"][0];
type Todo = {
title: string;
items:Array<{name:string,value:string}>;
description: string;
completed: boolean;
}
type TodoItems = Todo["items"][0];
//...
CodePudding user response:
I would do it in the other way:
interface TodoItem {
name: string
value: string
}
interface Todo {
title: string
items: TodoItem[]
description: string
completed: boolean
}
const todo: TodoItem = {
name: "Clean room",
value: "yes",
}