4

I want to know what is the right way to use types or interfaces without directly importing them (if that's an ok idea).

Currently I have this code:

import { TCollection } from "../__types__"; function doSomething(collection: TCollection) { // ... } 

So to get rid of an import statement I have tried to replace typeRoots inside of tsconfig.json with this value:

{ "compilerOptions": { ... "typeRoots": ["./src/__types__/types.d.ts", "./node_modules/@types"], } } 

And declared a type in ./src/__types__/types.d.ts:

declare type TCollection = { ... }; 

But still having an error:

function doSomething(collection: TCollection) { ^^^^^^^^^^^ Cannot find name 'TCollection'.ts(2304) } 

Did I made a mistake or is it an IDE bug or something? Thanks

2
  • Does it work, if you change your typeroots to: "typeRoots": ["./src/__types__", "./node_modules/@types"],? Commented Apr 14, 2020 at 14:41
  • nope, still the same Commented Apr 14, 2020 at 14:47

1 Answer 1

4

you can define the types on the global scope:

// global-types.d.ts import { Something } from "somewhere"; declare global { interface TCollection { ... } } 

if you do not need to import anything in your global-types.d.ts you can just declare the interface directly:

// global-types.d.ts interface TCollection { ... } 

no need for any tsconfig changes

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.