Adding custom types to Angular

Viewed 1398

I'm trying to add a custom types file to my angular project. I created typings.d.ts inside my Angular project (created with the CLI), inside the src folder. Then inside my tsconfig.json I have

"types": [
    "src/typings.d.ts"
]

but regardless of what I've tried, I always get this error. Though from what I've Google over the last hour has been types is for specific files whereas typeRoots are for type directories.

ERROR in ./src/app/profile-mobile/profile-mobile.component.ts

Module not found: Error: Can't resolve 'src/typings' in 'C:\Users\errat\Documents\Projects\chat\src\app\profile-mobile'

also weirdly: Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp'

Though these errors don't happen after adding the types file, it's only after importing the Type in an angular component to use it (I don't even have to be using it).

1 Answers

In the documentation of typescript there is

By default all visible ”@types” packages are included in your compilation

so you should not even add your declaration to tsconfig.json, it will work by default as long as you have this declaration under your root

The error, that you got, happens, because you try to import declaration file (*.d.ts) into component, it will not work like that, because declaration files is a separate type of files and you can not import them like usual esnext modules

if you have some interfaces, that you want to use, just create usual *.ts files and import them like usual module with import ... from ...

Related