Kyle Johnson
Kyle Johnson

Reputation: 961

Access an exported module in my d.ts file

I have a d.ts file in react-native

@types/react-native/index.d.ts

export interface ViewProps

I want to access that type within my d.ts file.

globals.d.ts

declare let View: React.ComponentType<ViewProps>;

The only way I've managed to make this work is to add the following to react-native/index.d.ts which I don't want to do.

Adjusted @types/react-native/index.d.ts

export as namespace ReactNative;

Is there a way to access the ViewProps interface without adjusting the type file in node_modules?

My solution

I think the answer from @barinbritva is correct, however for some reason my IDE (Webstorm) does not seem to like me importing any modules in the d.ts file, as soon as I add any import it seems to break any type detection I had.

patch the type file to export the namespace

@types+react-native+0.63.1.patch


diff --git a/node_modules/@types/react-native/index.d.ts b/node_modules/@types/react-native/index.d.ts
index d272ac7..2c4e92b 100644
--- a/node_modules/@types/react-native/index.d.ts
+++ b/node_modules/@types/react-native/index.d.ts
@@ -64,6 +64,8 @@
 /// <reference path="LaunchScreen.d.ts" />
 
 import * as React from 'react';
+// tslint:disable-next-line:export-just-namespace
+export as namespace ReactNative;
 
 type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

global.d.ts

declare let View: React.ComponentType<ReactNative.ViewProps>;

Upvotes: 0

Views: 689

Answers (1)

barinbritva
barinbritva

Reputation: 123

Which version of TypeScript do you use? If 3.8 and above try in your globals.d.ts import types from @types/react-native/index.d.ts using this:

import type { ViewProps } from 'react-native'

If TypeScript version is below then 3.8 you can do next:

import { ViewProps } from 'react-native'

Upvotes: 1

Related Questions