#Index signature for type 'string' is missing in type

6 messages · Page 1 of 1 (latest)

open schooner
#

Hi everyone,

I get following error when assigning p to person1 but not person. Seems like I am missing some key concept w.r.t. any and unknown. Can someone explain this? Thanks!
Typescript Playground
Type 'Person' is not assignable to type 'Record<string, unknown>'.
Index signature for type 'string' is missing in type 'Person'.(2322)

    name: string;
    age: number;
}

let p: Person = {
    name: 'Sam',
    age: 12
}

let person: Record<string, any> = p;
console.log(person);

// this is where I am getting error.
let person1: Record<string, unknown> = p;
console.log(person1);```
fast bronze
#

Please copy your code in TypeScript Playground and share link

#

Why do you want this type Record<string, unknown>?

open schooner
#

@fast bronze Added link to playground. Also, this is just for understanding the conversion in between types.

fast bronze
#

unknown is not the same as any https://stackoverflow.com/questions/51439843/unknown-vs-any

This in contrast works

interface Person {
    name: string;
    age: number;
    u?: unknown;
}

const person1: Record<string, unknown | any> = p;
open schooner