Exporting an extended interface to allow for interoperability

I have a problem with exporting an interface called Person. I recently added a new property to it, but when I try to export it, I encounter the following error message:

'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 someone please help me fix this issue?

Additional Information

Language:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3878a83968090819a8387b3c7ddc2ddc1">[email protected]</a>

Editor:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6a7f64664b3a253e38253b">[email protected]</a>

Editor Typescript package:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8a9bca7a5e5bcb1b8adbbabbaa1b8bc88f9fce6f9e6fa">[email protected]</a>
(which is using
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72060b02170111001b020632465c425c41">[email protected]</a>
)

I have already checked all the code and suggestions in VSC, but the issue still persists.

Answer №1

When using the export keyword by itself, it expects a value to be exported. However, in this case, the Person is just a type.

To address this issue, you can simply use export interface.

export interface Person { ... }

By exporting the interface (identified by its name), you are able to perform additional declaration merging as follows:

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

This will merge the different aspects of Person together and export it as a unified interface from the module.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message during ng Build Prod: Component declared in two modules when it should be in the same module

When running the ng build --prod command, I encountered an error message that is causing some confusion: The error states: Type SiteSecurity in "PATH"/siteSecurity.component.ts belongs to the declarations of 2 modules: SiteSecurityModule in "PATH"/s ...

Ways to retrieve the most recent update of a specialized typing software

When attempting to run typings install in a sample project with the below typings.js file, I received a warning. How can we determine the latest version number and what does the number after the + symbol signify? { "globalDependencies": { "core-js ...

incorrect implementation of react lifecycle phases

My Sharepoint Framework webpart includes a property side bar where I can choose a Sharepoint List, and it will display the list items from that list in an Office UI DetailsList Component. Although all REST calls are functioning properly during debugging, ...

Angular: use an icon in place of text

Consider this scenario where a component.ts file looks like the following: @Input() studentHeader?: BaseStudent; get studentText() { if(this.studentHeader) { return this.studentHeader.nr + ' - ' + this.studen ...

Issue with displaying Angular index.html page post-build

My Angular application runs smoothly on ng serve, but after building and uploading with ng build --prod, the index.html file fails to open. I've tried using various base href configurations like <base href="#">, <base href="/& ...

Why does the custom method only trigger once with the addEventListener?

I am attempting to connect the "oninput" event of an input range element to a custom method defined in a corresponding typescript file. Here is the HTML element: <input type="range" id='motivation-grade' value="3" min="1" max="5"> This i ...

Having trouble locating the name WebGLObject in my TypeScript code

Every time I try to run ng serve command An error pops up on my screen saying: "WebGLObject cannot be found." ...

Utilize variable as both a function and an instance of a class

Is there a way to access a variable as both a function and an object instance using TypeScript? log("something"); log.info("something"); I've attempted various methods, such as modifying the prototype, but nothing has proven succ ...

RxJS BehaviorSubject allows you to retrieve the current value or obtain a new one depending on a specific condition

I am managing a subject that consumers subscribe to: private request$: Subject<Service> = new BehaviorSubject(null); Upon initialization, my components utilize this function: public service(id: number): Observable<Service> { return this. ...

Utilizing NestJS to efficiently share an end-to-end server across multiple test suites

Currently, I'm utilizing the NestJS test module to simulate the nest app for testing purposes and my goal is to make this app accessible across various test suites. Here is how I have set it up: test |_ helpers |_ testApp.ts |_ e2e |_ u ...

Handling JSON data with Reactive Extensions in JavaScript

Hey everyone, I'm a beginner in Angular and RxJS coming from a background in VueJS. I've been struggling to grasp the inner workings of RxJS and would really benefit from some guidance from more experienced individuals regarding my current issue. ...

Extract nested values within objects and arrays, and return the complete type of the original object

I have a dataset that resembles the structure of IconItems: { title: "Category title", description: "Example description", lists: [ { id: "popular", title: "Popular", items: [ { ...

Sorting an array of objects in Typescript using a dynamic property name for generics

My goal is to develop a versatile, typed function that can arrange an array of objects by a numerical value housed under a dynamically named property within each object. Here is my current progress: export const sortByNumber = <T, K extends keyof T> ...

Error message: "Mismatched data types in Formik errors when utilizing TypeScript"

I have a customized input component for formik which includes an error label if one exists. When I print it like this: {errors[field.name]}, it works. However, {t(errors[field.name]?.toLocaleString())} does not work. import { FieldProps, FormikErrors } ...

GitHub Alert: Invalid format for environment variable

I am facing an issue with GitHub action. Despite configuring the README.md via a web browser, I encountered an error in GitHub action: Run export BUILD_VERSION=$(grep version package.json | awk -F \" '{print $4}') export BUILD_VERSION ...

Is it recommended to utilize the `never` type for a function that invokes `location.replace`?

I'm facing an issue with my TypeScript code snippet: function askLogin(): never { location.replace('/login'); } The TypeScript compiler is flagging an error: A function returning 'never' cannot have a reachable end point. Do ...

What is the best way to determine which function to invoke in ngIf?

Depending on the value of a variable, I need to call either the login() or logout() methods from this.loggedInService.isLoggedIn. If the value of the variable is !this.loggedInService.isLoggedIn, then call login(). If !this.loggedInService.isLoggedIn is ...

Uh-oh! An unexpected type error occurred. It seems that the property 'paginator' cannot be set

I am developing a responsive table using Angular Material. To guide me, I found this helpful example here. Here is the progress I have made so far: HTML <mat-form-field> <input matInput (keyup)="applyFilter($event.target.value)" placeholder ...

What is the best way to troubleshoot a quasar typescript file type error?

Currently, I am delving into Quasar using TypeScript and encountering a type error while working on file uploads. Here is the snippet of my code where the type error arises specifically in the parameter of the form.append() method. The error message read ...

Drag and drop functionality in Angular 2 using TypeScript

Has anyone experimented with the drag and drop functionality using Angular2 RC with TypeScript? Thanks, Sanket ...