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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With