The correlation between a TypeScript class and an interface bearing an identical name

I have been struggling to find clear documentation or explanation for the unique relationship between a TypeScript class and an interface that share the same name.

  • What is the significance of having an interface with the same name as a class?
  • Why does a class automatically implement an interface with the same name?
  • Why does the compiler raise errors when implementing readonly fields in a class and interface with matching names, but not when the names are different?
  • Is there any official documentation that addresses these specific questions?

Code:

// Co-named interface and class doesn't like readonly property implementation:

interface Foo {
  readonly x: number; // Error: Duplicate identifier 'x'
  y: number;
}

class Foo {
  get x(): number { // Error: Duplicate identifier 'x'
    return 0;
  }

  y = 1;
}

// Same as above, but different class name + explicit `implements`

class Bar implements Foo {
  get x(): number { // No error!
    return 0;
  }

  y = 1;
}

// Duplicating the first example, but explicitly implementing the co-named interface:

interface Baz {
  readonly x: number; // Error: Duplicate identifier 'x'
  y: number;
}

class Baz implements Baz {
  get x(): number { // Error: Duplicate identifier 'x'
    return 0;
  }

  y = 1;
}

Answer №1

When you have interfaces with the same name within a module, they will be combined:

interface Dog {
    breed: string;
}

interface Dog {
    age: number;
}

let myDog = {} as Dog;
myDog.breed; // Works fine
myDog.age; // Works fine too

Every time you declare a class, you are essentially creating both a constructor function and a type declaration, making classes usable as interfaces.

class Cat {
    color: string;
}

interface ICat extends Cat { } // includes color property

class MyCat implements Cat {
    color: string = "black";
}

If you happen to have a class and an interface with the same name, it is equivalent to having two interfaces with the same name. If both instances of the interface define the same members but with different types, conflicts can arise.

It's interesting that Typescript permits this scenario:

export interface Person {
    readonly name: string;
}

export class Person {
    readonly name: string = "John";
}

However, defining get name() { return "John"; } is not allowed even though both produce as readonly name: string. The reason behind this is possibly due to the type checker treating them as distinct entities during merging, despite their semantic similarity (this explains why extending the interface and specifying the readonly property as a getter method is permissible).

Answer №2

This issue specifically pertains to accessors, and as of now, the Typescript team has not provided any official comments on it.

class Foo {
  readonly x: number = 0;
}

class Bar extends Foo {}
interface Bar {
  readonly x: 2;
}

const bar = new Bar();
bar.x; // type is 2, no error

The team did make a comment in response stating that "[w]hether a property is implemented as a field or a getter/setter pair is an implementation detail, not part of the type," however, in Typescript 4.2 this statement seems invalid:

class Baz extends Foo {
    // error: "'x' is defined as a property in class 'Foo',
    //         but is overridden here in 'Baz' as an accessor."
    get x() {
        return 2;
    }
}

I am unsure why the team has responded like this. The mention that getters/setters are not considered part of the type system dates back to 2015 and could possibly be outdated.

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

Is TypeScript necessary, or can I simply stick with ES6?

As a client developer using AngularJS in my daily job, we are considering transitioning to TypeScript. After researching TypeScript, I discovered that most JavaScript packages I require need definition type files. This can be inconvenient, especially whe ...

Explanation of TypeScript typings for JavaScript libraries API

Currently, I am in the process of building an Express.js application using TypeScript. By installing @types and referring to various resources, I managed to create a functional program. However, my main query revolves around locating comprehensive document ...

Angular 7's innovative method for handling singleton instances: Dynamic provider

In my Angular application, I have the ability to display lists of videos or articles along with their details. There are two main components: ContentListPage and ContentDetailsPage, which serve the same purpose for both videos and articles. The only diff ...

Conceal Primeng context menu based on a certain condition

I'm struggling to prevent the context menu from showing under certain conditions. Despite following the guidelines in this post, the context menu continues to appear. My goal is to implement a context menu on p-table where it should only show if there ...

Collection of functions featuring specific data types

I'm currently exploring the idea of composing functions in a way that allows me to specify names, input types, and return types, and then access them from a central function. However, I've encountered an issue where I lose typing information when ...

Developing a Universal Type in Typescript

Encountered an issue with generic types while working on a user-defined type(interface) structured like this: IList1: { prop1: string, prop2: number, prop3: string . . . } ILi ...

Comparing vue.component to using import statements inside a component

After setting up a vue2 library using vue-cli, I have numerous components included in the index.ts file as shown below: import MyComponent1 from './components/MyComponent1.vue'; import MyComponent2 from './components/MyComponent2.vue'; ...

Using Typescript to extract the type from within the function type parameters

I am currently utilizing an imported type definition that contains a function type definition with a fairly complex parameter type: export interface SomeTypeDefinition { someFunction: ( param1: string, param2: { key1: number, key2: s ...

String interpolation can be used to easily accept numbers with dot separators

Is it possible to create a function that can accept numbers with dot separators? Here are some examples: func('100.000.002.333') // should pass func('10') // should pass func('20') // should pass func('100') // shou ...

Converting hexadecimal to binary using Javascript or Typescript before writing a file on an Android or iOS device

Hey everyone! I'm facing a puzzling issue and I can't seem to figure out why it's happening. I need to download a file that is stored in hex format, so I have to first read it as hex, convert it to binary, and then write it onto an Android/ ...

Utilize the TypeScript Compiler API to extract the Type Alias Declaration Node from a Type Reference Node

Currently, I am utilizing ts-morph library which makes use of the TS Compiler API. Here is an example of my code: export type Foo = string export const foo: Foo = 'bar' Whenever I try to find the type for the export of foo, it returns string. H ...

Generating a composer method in TypeScript (Flow $Composer)

While flow supports $Compose functions, the equivalent mechanism seems to be missing in TypeScript. The closest thing I could find in TypeScript is something like https://github.com/reactjs/redux/blob/master/index.d.ts#L416-L460. Is there a native equivale ...

Is it possible to omit certain fields when using the select function in MikroORM?

When working with nested populate queries in MikroORM with MySQL, I am faced with the challenge of selecting 100 fields while wanting to exclude around 20 fields. It would make more sense to leave out those 20 fields, similar to using db.find().select("- ...

Step-by-step guide to initializing data within a service during bootstrap in Angular2 version RC4

In this scenario, I have two services injected and I need to ensure that some data, like a base URL, is passed to the first service so that all subsequent services can access it. Below is my root component: export class AppCmp { constructor (private h ...

Updating the value of the chosen drop down option upon selection

I currently have a Material UI dropdown menu implemented in my project. My goal is to use the selected option from the drop down menu for future search functionality. How can I utilize onChange() to store the selected option effectively? At the moment, I ...

I am experiencing an issue where my code is not iterating over the data in my

The issue I'm facing with the code below is that it only displays the quantity of the first item, rather than all items in my shopping cart. import {ShoppingCartItem} from './shopping-cart-item'; export class ShoppingCart { constructor ...

Using Array.push within a promise chain can produce unexpected results

I have developed a method that is supposed to retrieve a list of devices connected to the network that the client has access to. export const connectedDevicesCore = (vpnId: string, vpnAuthToken: string) => Service.listVPNConnectionsCore ...

Obtain a segment of the string pathway

In this scenario, there is a file path provided below. Unlike the rest of the URL, the last part (referred to as video2.mp4) regularly changes. The language used for this project is either Javascript or Typescript. file:///data/user/0/com.sleep.app/files/ ...

Webpack is failing to recognize certain CSS files

My development stack includes Vue.js 2.5.15, Webpack 4.12.0, css-loader 0.28.11, ASP.Net Core 2.1 in Visual Studio 2017. Starting with the Visual Studio asp.net core template project for Vue and Typescript, I prefer to have individual small CSS files with ...

Efficient Searching with Typescript and Lodash: Boosting Performance with Arrays and Objects

I am faced with the challenge of converting between two classes called MyObject and MyObjectJSON, which have helper methods to assist in the conversion process: myObj.toJSON() and MyObject.fromJSON(). Currently, I have instances of these classes represent ...