Panda Noir

JavaScript の限界を究めるブログでした。最近はいろんな分野を幅広めに書いてます。

satisfies は msw でモックを書くときとかに使える

import { setupServer } from 'msw/lib/node';
import { rest } from 'msw';

type ArticleResponse = { title: string; id: string };
setupServer(
  rest.post('/article', (_req, res, ctx) =>
    res(
      ctx.json({ title: 'mocked article', id: 'id' } satisfies ArticleResponse)
    )
  )
);

satisfies を使えない場合、const response: ArticleResponse = { /* */ } のように一時変数を用意する必要がある。

こんな感じで、unknown あるいは any を受け付けているところに制約を設ける目的で使うことができる。