user1365697
user1365697

Reputation: 5999

How to use export type in TypeScript?

I used a library from NPM

https://www.npmjs.com/package/yaml

and this his the @types/yaml

https://www.npmjs.com/package/@types/yaml

In my source code I did import to

import * as yaml from 'yaml';

but I don't have access to YAMLError it is define in the @types/yaml

export type YAMLError =
    | YAMLSyntaxError
    | YAMLSemanticError
    | YAMLReferenceError;


https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/yaml/index.d.ts

Can I use export type ?

Upvotes: 2

Views: 280

Answers (1)

In your current situation, you would need to use yaml.YAMLError as you've namespaced your import.

You could do:

import { YAMLError } from 'yaml'

It "should" automatically retrieve the correct type information for you.

Depending on your editor or IDE, it can do automatic imports for you if you have the right plugins like the TypeScript Language Server.

export type is to make your new type available to the rest of your code and be able to import it. If you were to make an addition to the existing type or a completely new one based of it like this example:

export type MyYAMLError = YAMLError | null

You could start using MyYAMLERROR in your own code. Not sure if this is what you're looking for so I suggest redoing your imports as described above.

Upvotes: 3

Related Questions