RamoFX
RamoFX

Reputation: 536

Exporting interface which is extended

I have interface Person in which I added another property (see example below).

I want to export it, however I ger error 'Person' only refers to a type, but is being used as a value here..

// test.d.ts
interface Person {
  age: number,
  name: string
}

interface Person {
  canCode: boolean
}

export Person // => 'Person' only refers to a type, but is being used as a value here.
// test.ts
import { Person } from './test' // => Module '"./test"' declares 'Person' locally, but it is not exported.

const myPerson: Person = {
  age: 17,
  name: 'Roman',
  canCode: true
}

Can you tell me please, how to fix it?

Additional info

Lang: [email protected]

Editor: [email protected]

Editor Typescript package: [email protected] (which is using [email protected])

Also I checked all the code and suggestions in the VSC and the results are the same.

Upvotes: 1

Views: 258

Answers (1)

Mats Jun
Mats Jun

Reputation: 351

The export keyword on its own expects a value to export, but as you mentioned, Person is only a type.

To solve this, you simply just use export interface.

export interface Person { ... }

The interface (identified by its name) is now exported. You can perform further declaration merging by adding for example:

export interface Person { age: number }
interface Person { name: string }

Person will be merged together and exported as a single interface from the module.

Upvotes: 1

Related Questions