TS2688 with TypeScript, React Native, and MobX

To preserve my sanity if I should run into this again…. 

I created a React Native project, ejected it, and then converted it to use TypeScript. Things were going well until I started to use MobX. The following error occurred:

node_modules/mobx/lib/utils/utils.d.ts(1,23): error TS2688: Cannot find type definition file for 'node'.

Something as simple as:

import { observable } from "mobx";
may cause the error shown above. If you look at the utils.d.ts file mentioned in the TS2688 error line, you’ll see what’s causing the error:
/// <reference types="node" />
It seems like a harmless error and easily resolved. Usually when there are missing types, your first step should be to add them to your project. My first attempt was to add the @types/node to the project.
yarn add -D @types/node
Bzzt. That produces a whole new batch of errors due to duplicate definitions. There are quite a few classes and functions that are duplicates when React Native and Node declarations are both imported.
Apparently, this needs a stub for node so that the error can be bypassed (without changing or using other compiler switches like –skipLibCheck).
I created a new folder in the src tree called typings. In that folder, I created another folder, this time called node. And finally, in that folder, I created a nearly empty file called index.d.ts (the file contains a comment about why I created the file and a link to this blog post).
Folder structure
The project now compiles without issue. The declaration types that were needed by MobX are already included elsewhere, but the pesky reference directive forces the compiler to try to locate the types.

2 Comments

  1. looks like it’s an issue with mobx 3.6.x I ran into the same error and tried compiling on 3.5.1 and it doesn’t throw the error. I’ve posted an issue in the mobx repo. Thought I’d give you a heads up.

    cheers,
    foltz

Comments are closed.