Заметил, что многие пропустили появление satisfies в TS 4.9, а штука-то классная.

Объясняю на пальцах. Иногда нам нужно убедиться, что объект соответствует какому-то типу, но при этом нужно сохранить специфичность типа.





type TestType = {a: number | string; b?: string};



const testObject = {a: 1} as TestType;



testObject.a.toFixed(); // Ошибка: Property 'toFixed' does not exist on type 'string'.







Используем satisfies







type TestType = {a: number | string; b?: string};



const testObject = {a: 1} satisfies TestType;



testObject.a.toFixed(); // Никаких ошибок



testObject.b?.toUpperCase(); // Ошибка: Property 'b' does not exist on type '{ a: number; }'