TS2352

error TS2352: Conversion of type ‘{ name: string; age: number; }‘ to type ‘Person‘ may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to ‘unknown‘ first.

Broken Code ❌

1
2
3
4
5
6
7
8
9
type Person = {
firstName: string;
age: number;
};

const myObject = {
name: 'Benny',
age: 34,
} as Person;

Fixed Code ✔️

Make sure all properties of your object match the properties of your declared type:

1
2
3
4
5
6
7
8
9
type Person = {
firstName: string;
age: number;
};

const myObject = {
firstName: 'Benny',
age: 34,
} as Person;

Alternative but not recommended: Convert your object to unknown first:

1
2
3
4
5
6
7
8
9
type Person = {
firstName: string;
age: number;
};

const myObject = {
name: 'Benny',
age: 34,
} as unknown as Person;