unsplash-js: `createApi` errors when TypeScript `dom` `lib` is not included

Hello πŸ‘‹

I am integrating with unsplash for a project. While I was successful I found the following problem:

Steps to Reproduce

Use createApi in Typescript projects.

Observed Behaviour

Screen Shot 2021-03-16 at 4 29 45 PM

In order to use Typescript I ended up doing:

// @ts-ignore
const unsplash = createApi({
  accessKey: functions.config().unsplash.access_key,
  fetch,
});

Expected Behaviour

createApi code should be valid with Typescript codebase if following documentation instructions.

Technical Notes

"unsplash-js": "^7.0.4"
"typescript": "^3.8.0"

I also tried with typescript v4, yielding same result.

Thank you, let me know if I can provide more info.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

i found a solution that using cross-fetch, will solve the typings issue for fetch type from createApi interface.

import nodeFetch from 'cross-fetch';

I just pushed a fix. With v7.0.6 you should be able to do this:

import * as nodeFetch from 'node-fetch';
import { createApi } from 'unsplash-js';

declare global {
    var fetch: typeof nodeFetch.default;
    type RequestInit = nodeFetch.RequestInit;
    type Response = nodeFetch.Response;
}

const unsplash = createApi({
    accessKey: 'some access key',
});

export default unsplash;

This is only necessary if you’re not using the dom lib.

https://github.com/unsplash/unsplash-js/compare/v7.0.5...v7.0.6