Got some code in ./fooApi.ts
. But I have a mock for it in __mocks__
-- this is a way to make sure that the mock version of your code is imported when using mocks in Jest. This means that when the code runs the actual type of the thing imported will be jest.Mock
. But TS doesn't know about the mocks. So I need to cast them with as
:
import { createUrl, getToken } from './fooApi';
const mockedCreateUrl = createUrl as jest.Mock;
const mockedGetToken = getToken as jest.Mock;
OK TS is happy with that. But I'm wondering if I can do it in one line. I don't see anything on the type assertions page about this.
Read this answer here and tried:
import { createUrl, getToken } : {createUrl: jest.Mock, getToken: jest.Mock } from './fooApi';
Which resulted in TS1005: 'from' expected.
at the first colon.
Any way to turn those 3 lines into one?