What is the best way to implement type discrimination in TypeScript?

When using TypeScript with two interfaces combined as a union type, why doesn't it distinguish which type members can be declared?

interface IFish {
  swim: () => void;
}

interface ICat {
  meow: () => void;
}

type Pet = IFish | ICat;

const pet: Pet = {
  meow: () => {
    //
  },
  swim: () => {
    //
  }
};

Answer №1

When using interfaces, you are specifying the members that an object must have, but not restricting which members it cannot have. This means that an IFish can have a swim method and vice versa.

Let's examine a revised example:

interface IFish {
    swim: () => void;
    meow: undefined;
}

interface ICat {
    meow: () => void;
    swim: undefined;
}

type Pet = IFish | ICat;

const pet: Pet = {  // error in compilation
    meow: () => {
        //
    },
    swim: () => {
        //
    }
};

In this demonstration, we explicitly declare that an IFish should not have a meow method, while an ICat should not have a swim method. The downside to this approach is that managing multiple interfaces and methods may require adding numerous instances of undefined to your type definitions.

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

Issue with Angular ngFor binding. What could be causing this error to occur?

I have a main component called DOCUMENT. This document receives a URL segment and retrieves an array of associated objects from my database. Then, using @Output() documents = new EventEmitter() and an @Input() in a DOCUMENT VIEW component, I loop through t ...

Locate a specific item by its ID within a JSON file utilizing Angular version 2 or later

My JSON file structure is like the example below: { "id": "1", "country": "Brazil", "state": [ {"id": "1", "name": "Acre", "city": [ { "id": "1", "name": "Rio Branco"}, { "id": "2", "name": "Xapuri"} ...

Remove a record from Angular 2 Firebase collection

I've been searching extensively for a solution to this problem. Despite following the documentation on AngularFire 2 and Angular 2, I am unable to find a working answer. My goal is simply to delete a specific entry in my Firebase database using its un ...

What is the correct data type for the vuex store that is passed to the vuex plugin when it is being initialized?

Here is how the store initialization process is currently being implemented: /* Logic */ import Vue from 'vue' import Vuex, { StoreOptions } from 'vuex' Vue.use(Vuex) const DataManager = new UserDataManager() type RootStateType = { ...

How can Typescript elevate your use of the IFrame API?

Here is a code snippet that performs the following actions: let doc = this.iframe.contentDocument || this.iframe.contentWindow; let content = `...` doc!.open(); doc!.write(content); doc!.close(); Despite this, the Typescript linter thr ...

Utilizing ngx-translate in Angular6 to dynamically load translations by making an API request to the backend

Using ngx-translate in my frontend, I aim to dynamically load translations upon app launch. The backend delivers a response in JSON format, for example: { "something: "something" } Instead of utilizing a local en.json file, I desire to integrate thi ...

What is the trick to maintaining the chiplist autocomplete suggestions visible even after inserting or deleting a chip?

After creating an autocomplete chiplist component in Angular, I noticed that when a user adds or removes an item, the suggestion list disappears and the input field is cleared. However, I would prefer to keep the autocomplete list open (or reopen it) so t ...

Please come back after signing up. The type 'Subscription' is lacking the specified attributes

Requesting response data from an Angular service: books: BookModel[] = []; constructor(private bookService: BookService) { } ngOnInit() { this.books = this.fetchBooks(); } fetchBooks(): BookModel[] { return this.bookService.getByCategoryId(1).s ...

Is there a way to ensure DRY principles are followed while utilizing Redux Toolkit's asyncThunkCreator?

I have a query related to RTK. I find myself repeating code in my action creators created with createAsyncThunk because I want to be able to cancel any request made. I am thinking of creating a wrapper to streamline this process, but I am facing challenge ...

Is there a way to display a variety of images on separate divs using *ngFor or another method?

I have created a custom gallery and now I would like to apply titration on the wrapper in order to display a different image on each div. Currently, my code is repeating a single image throughout the entire gallery. HTML <div class="wrapper" ...

Angular 4 offers a variety of template options to choose from

Imagine these divs within a component: <div ngIf="condition1"> <button (click)="set_condition1_false_and_2_true()">show-template-2</button> </div> <div ngIf="condition2> <button (click)="set_condition2_false_and_3_ ...

How can time duration be accurately represented in TypeScript?

As I work on designing an interface for my personal project, I am in need of adding a field that represents the length of time taken to complete a task. What data type and format should I use in TypeScript to express this? Below is the interface I have cr ...

Utilizing TypeDoc to Directly Reference External Library Documentation

I am working on a TypeScript project and using TypeDoc to create documentation. There is an external library in my project that already has its documentation. I want to automatically link the reader to the documentation of this external library without man ...

When receiveMessage is triggered, the FCM push notification fails to refresh the view

After following this helpful tutorial on Push Notifications with Angular 6 + Firebase Cloud Messaging, everything is working perfectly. I am able to receive notifications when using another browser. To ensure that my notification list and the length of no ...

Display the modal in Angular 8 only after receiving the response from submitting the form

I am encountering an issue where a pop-up is being displayed immediately upon clicking the submit button in Angular 8, before receiving a response. I would like the modal to only appear after obtaining the response. Can someone assist me with achieving thi ...

What is the best approach to managing exceptions consistently across all Angular 2/ Typescript observables?

Throughout the learning process of Angular2, I have noticed that exceptions are often caught right at the point of the call. For example: getHeroes(): Promise<Hero[]> { return this.http.get(this.heroesUrl) .toPromise() ...

Issue with bootstrap modal new line character not functioning properly

Is there a correct way to insert a new line for content in a modal? I have this simple string: 'Please check the Apple and/or \nOrange Folder checkbox to start program.' I placed the '\n' newline character before "Orange," e ...

Unexpected token in catch clause in React Native TypeScript

Despite having a fully configured React Native Typescript project that is functioning as expected, I have encountered a peculiar issue: Within all of my catch blocks, due to the strict mode being enabled, typescript errors are appearing like this one: My ...

Navigating the correct way to filter JSON data using HttpClient in Angular 4

Struggling with transitioning from Http to HttpClient, here's the code in question: constructor(public navCtrl: NavController, public http: HttpClient, private alertCtrl: AlertController) { this.http.get('http://example.com/date.php') .su ...

Broadening Cypress.config by incorporating custom attributes using Typescript

I'm attempting to customize my Cypress configuration by including a new property using the following method: Cypress.Commands.overwrite('getUser', (originalFn: any) => { const overwriteOptions = { accountPath: `accounts/${opti ...