TS2352

error TS2352: Conversion of type ‘’ to type ‘’ may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to ‘unknown’ first.

Bad Code ❌

1
2
3
4
5
6
7
8
9
interface MyInterface {
firstName: string;
age: number;
}

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

Fixed Code ✔️

Stick to the properties of the interface (recommended):

1
2
3
4
5
6
7
8
9
interface MyInterface {
firstName: string;
age: number;
}

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

Alternative: Convert your object to unknown first (not recommended):

1
2
3
4
5
6
7
8
9
interface MyInterface {
firstName: string;
age: number;
}

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