Type narrowing is ineffective for discriminated unions in most cases

Consider the type definition provided below:

type A = { a: string } | { a?: undefined; b: string }

This essentially means that if you include a, it should be the only property provided. If you do not include or have a as undefined, then you also need to provide b.

The goal is to access b when a is undefined in the code snippet below:

let t = true
// The intention with this line of code is to confuse the compiler so it doesn't narrow down the definition just yet.
const z: A = t ? { a: 'a' } : { b: 'b' }

if (z.a) {
  console.log(z.a)
} else {
  console.log(z.b)
}

However, an error message is received:

Property 'b' does not exist on type 'A'.
  Property 'b' does not exist on type '{ a: string; }'

You can find a link to TypeScript Playground with the relevant code.

I'm searching for a type-safe solution without using unsafe methods like hasOwnProperty('b') or type assertions. Is there a way to achieve this?

Answer №1

Encountered the following error message:

The property 'b' does not exist on type 'A'.
  Property 'b' does not exist on type '{ a: string; }'

The reason for this error is that it's possible to enter the else block and still have a string in the z.a property. This occurs when z.a is an empty string "". Therefore, the type remains unchanged in the else case.

If you modify the code as follows, it will work correctly:

if (typeof z.a === 'string') {
  console.log(z.a)
} else {
  console.log(z.b)
}

Playground link

Answer №2

You need to make your check more specific:

if (z.a !== null) {
  console.log(z.a)
} else {
  console.log(z.b)   // this will work
}

The issue with your code lies in the fact that z.a could potentially be an empty string, which is a falsy value. This would cause the program to take the else branch even if z.b does not exist...

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

Unidentified Controller Scope in Angular and TypeScript

I am struggling with my Angular 1.5 app that uses Typescript. Here is a snippet of my code: mymodule.module.ts: angular.module('mymodule', []).component('mycomponent', new MyComponent()); mycomponent.component.ts export class MyCont ...

When a file is modified in Angular, it triggers an error prompting the need to restart the 'npm' service

Whenever I make changes to a file in my Angular application, I encounter the following error: ERROR in ./src/app/@theme/components/auth/index.js Module build failed: Error: ENOENT: no such file or directory, open 'C:\Dev\Ng\ngx-a ...

Issue with validating the date picker when updating user information

Trying to manage user accounts through a dialog form for adding and updating operations, where the type of operation is determined by a variable injected from the main component. Encountered an issue while launching the update process - the date picker tri ...

Having trouble running `npm start` on my NodeJs project

Hi everyone, I could really use some help with the npm start command. I am currently working on a Node.js project with TypeScript on Windows 7-64, but I'm encountering errors when trying to start it. If you can assist, please check out the following ...

What sets apart `this.user.id` from `this.user = {id: ....}`?

I am puzzled by the error thrown in the code below: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-user', templateUrl: './user.compone ...

Leverage the Angular2 component property when initializing a jQuery function

I'm currently developing a web app with Angular 2 and utilizing jQuery autocomplete. When making requests to the remote server for completion data, I found that the server address is hardcoded in the autocomplete function. Even though I tried using co ...

Tips for sending a parameter to an onClick handler function in a component generated using array.map()

I've been developing a web application that allows users to store collections. There is a dashboard page where all the user's collections are displayed in a table format, with each row representing a collection and columns showing the collection ...

Set the timezone of a Javascript Date to be zero

Is there a way to create a Javascript date without any specific timezone? When I try to do so in Javascript, it automatically sets it to GMT Pacific standard time. let newDate = new Date(new Date().getFullYear(), 0, 2, 0, 0, 0, 0) }, newDate: Sat Feb 01 2 ...

Increasing a number after a delay in an Angular 2 AppComponent using TypeScript

I'm attempting to create a straightforward Angular2 Application with TypeScript. Despite its apparent simplicity, I'm struggling to achieve my desired outcome. My goal is to display a property value in the template and then update it after 1 sec ...

Discovering a specific string within an array of nested objects

Seeking guidance on creating a dynamic menu of teams using JavaScript/TypeScript and unsure about the approach to take. Here is an example dataset: const data = [ { 'name': 'Alex A', 'agentId': '1225& ...

The TypeScript error states, "Object does not contain property 'name'."

Can someone help me with this array.map function I'm trying to use: {props.help.map((e, i) => { return <a key={i}>{e.name}</a> })} The {e} object contains keys 'name' and 'href' I keep getting an error message that ...

Automatically select the unique item from the list with Angular Material AutoComplete

Our list of document numbers is completely unique, with no duplicates included. I am attempting to implement a feature in Angular Material that automatically selects the unique entry when it is copied and pasted. https://i.stack.imgur.com/70thi.png Curr ...

Searching for true values in MongoDB using the query syntax can be challenging

I have a question that might be a bit embarrassing, but I need help with rendering a user search based on both location and date. Our profile object is structured like this: availability: { monday: { type: Boolean, default: false }, tuesday: { type ...

Error: NullInjector - The injector encountered an error when trying to inject the Component into the MatDialogRef in the AppModule

When utilizing a component via routing as well as injecting it as the "target" of a modal dialog, I encountered an issue: export class Component1 implements OnInit { constructor(private service: <someService>, public dialogRef: MatDialogRef<Compo ...

What causes a typed string literal to be recognized as a pure string in TypeScript?

Consider the TypeScript code below: type example = 'BOOLEAN' | 'MULITSELECT' | ''; interface IObjectExample { a: string, readonly b: example } const handleObj = (obj: IObjectExample) :void => { console.log(&ap ...

Using React to simulate API calls outside of testing environments

For instance, I encounter issues when certain endpoints are inaccessible or causing errors, but I still need to continue developing. An example scenario is with a function like UserService.getUsers where I want to use fake data that I can define myself. I ...

Issue: When attempting to read the length of a property in Angular 6, a TypeError is being thrown because the property is

Unable to retrieve values from an array using the TS code below: this.dataservice.get("common/public/getAllCategories", null).subscribe(data => { //console.log('categories'+JSON.stringify( data)); this.categoriesarray = data; }); var ...

Empty nested Map in POST request

I am currently working on a springboot application with a React/Typescript frontend. I have defined two interfaces and created an object based on these interfaces. export interface Order { customer_id: number; date: Date; total: number; sp ...

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 ...

The compilation of @types/socket.io-redis fails because it cannot locate the Adapter exported by @types/socket.io, which is necessary for its functionality

It seems like there may be an issue with my tsconfig file or something similar. npm run compile > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c69626b6562694c3d223c">[email protected]</a> compile /User ...