The ability to reach a variable beyond its block

As a newcomer to TypeScript (coming from the Java world), I am experimenting with creating a mapper function like this:

public getApercuTypePrestationFromTypePrestationEX044(typePrestationEX044: TypePrestationEX044): ApercuTypePrestation {
        let apercuTypePrestation: ApercuTypePrestation;
        if (null != typePrestationEX044) {
            apercuTypePrestation = new ApercuTypePrestation();
            apercuTypePrestation.codeTypePrestation == typePrestationEX044.code;
            apercuTypePrestation.libelleTypePrestation == typePrestationEX044.libelle;
        }

        console.log("A = " + typePrestationEX044.code);
        console.log("B = " + apercuTypePrestation.libelleTypePrestation);

        return apercuTypePrestation;
    }

Unfortunately, it's not working as intended. The console output shows: A = A8C B = undefined

Can anyone help me figure out how to fix this issue?

Answer №1

Make sure to use = instead of ==. I've updated the code from == to =, and it should work correctly now.

public getApercuTypePrestationFromTypePrestationEX044(typePrestationEX044: TypePrestationEX044): ApercuTypePrestation {
        let apercuTypePrestation: ApercuTypePrestation;
        if (null != typePrestationEX044) {
            apercuTypePrestation = new ApercuTypePrestation();
            apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
            apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
        }

        console.log("A = " + typePrestationEX044.code);
        console.log("B = " + apercuTypePrestation.libelleTypePrestation);

        return apercuTypePrestation;
    }

Remember, in typescript, == or === is for comparisons only, not for assignment. Use = to assign values.

UPDATE

I also noticed that you are checking typePrestationEX044 for null incorrectly.

Replace this:

if (null != typePrestationEX044) {
            apercuTypePrestation = new ApercuTypePrestation();
            apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
            apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
        }

With This

if (typePrestationEX044) {
            apercuTypePrestation = new ApercuTypePrestation();
            apercuTypePrestation.codeTypePrestation = typePrestationEX044.code;
            apercuTypePrestation.libelleTypePrestation = typePrestationEX044.libelle;
        }

This if condition will handle undefined, null, or boolean values automatically.

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

Creating dynamic components from JSON elements does not trigger a rerender of components within an array

Imagine having a simplified input structure like this: [ { type: "text", text: "how are you {name}" }, { type: "input", input: "name" }, { type: "text", text: "good to ...

Exploring the possibilities of Ionic 2 with Web Audio API

I am encountering issues while using the Web Audio API with Ionic 2. Despite my efforts, I keep running into errors. It seems that the problem lies within the TypeScript compiler. I attempted to resolve it by adding "es2015.promise", but to no avail. The e ...

What is the best way to attach a DOM element to an Angular 2 TestBed?

I was wondering if it is possible to add a DOM element to the test DOM before running tests. I'm new to testing, but something like fixture.debugElement.prepend('div') might work. However, I understand that this approach may indicate a desig ...

What steps do I need to take in order to set up InfluxDB with Nest

As a beginner in the world of software development, I am eager to expand my knowledge and skills. Has anyone had experience operating influxdb with nestjs? If so, I would greatly appreciate it if you could share your past experiences. Thank you for takin ...

The combination of Angular's ngrx and Router.Events within Rxjs does not seem to function as intended

I'm facing a challenging problem that I can't seem to resolve: onSelectCompany() { combineLatest([this.idCompany$, this.idUser$, this.router.events]).subscribe(res => { if(res[2] instanceOf NavigationEnd){ this.router.navigateByUrl(`g ...

``There seems to be an issue with the Deno logger FileHandler as it

I am currently in the process of setting up loggers for an application. I have a handler named console which logs every event to the console. Additionally, there is a handler called app that is supposed to log all events to a file. While the logs are succ ...

Exploring Objects with Union Types in TypeScript

Looking to iterate through an object that combines two interfaces. interface Family { cat: string; age: string; family: string; lastYearFamily: string; } interface Model { cat: string; age: string; ...

Unexpected halt in execution - VS Code Logpoint intervenes abruptly

Recently, I delved into the world of JavaScript/TypeScript development in VS Code. Intrigued by Eclipse Theia, I decided to explore it further by setting up a backend service. To track its execution, I added a logpoint to my backend service to see when it ...

How can I uniquely combine a code with an existing CSS class and make modifications to it?

I am using ngx-skeleton-loader and I would like to change the color, but I am facing some difficulties. Here is an image that illustrates the issue. When looking at the developer tools, you can see the styles action in the styles action bar. .loader ...

Encountering an issue with compiling Angular due to a Type Inference error

interface Course { name: string; lessonCount: number; } interface Named { name: string; } let named: Named = { name: 'Placeholder Name' }; let course: Course = { name: 'Developing Apps with Angular', lessonCount: 15 }; named = ...

Class property in Typescript is initialized in the constructor, but is undefined in a member function

I'm encountering a recurring problem while developing an Electron application using Typescript. The backend has a set of controllers, with the AppController managing file system interactions and WindowController handling basic window functions. Here&a ...

Unable to assign to 'disabled' as it is not recognized as a valid attribute for 'app-button'

How to link the disabled property with my button component? I attempted to add 'disabled' to the HTML file where it should be recognized as an input in the button component (similar to how color and font color are recognized as inputs) ... but ...

Exploring a Component's props and their data types

As a newcomer to React and Typescript, I have a straightforward question that I can't seem to find an answer to. I'm attempting to construct a tab layout using Typescript with headless UI following the documentation here I am encountering issue ...

The TypeScript error occurs when attempting to assign a type of 'Promise<void | Object>' to a type of 'Promise<Object>' within a Promise.then() function

I'm currently working on a service to cache documents in base64 format. The idea is to first check sessionStorage for the document, and if it's not there, fetch it from IRequestService and then store it in sessionStorage. However, I've encou ...

Using Typescript with Momentjs: 'String type cannot be assigned to Date type'

Up until now, my approach to utilizing the momentjs library was as follows: import * as moment from 'moment'; // import moment. @Component({..}); export class TestClass { lastUpdated = Date constructor(private myService: MyService){ this ...

Exploring the usage of array map parameters in rxjs 6 when combined with withLatestFrom

Prior to Rxjs 6, we were able to achieve the following: interface TypeA { payload: any; } source$.pipe( withLatestFrom(source2$, (source1: TypeA, source2: TypeB) => ({ payload: source1.payload, source2 }) ), ) In the resultSelector method ...

After successfully logging in, the deployed server encounters an Error 503 and shuts down. However, on the development environment, everything runs smoothly as

I am currently in the process of developing an application using NET 6 LTS and Angular 14. Everything runs smoothly on my development environment with IIS express. However, once I deploy the application (release version) on Windows 2019 with IIS 10, I enco ...

Errors in Visual Studio regarding typescript are often not found by tsc and eslint, causing frustration for developers

Today, after restarting my computer and launching visual studio code, I encountered an unfamiliar error that I've never seen before: https://i.sstatic.net/z1vw5.png I haven't made any changes to my project's code (confirmed by running git ...

Using Angular Typescript, implement a live chat feature that generates link previews

I am having trouble creating a link preview in live chat on my website. I want the preview of a dropped URL to show in the chat window, which requires extracting meta-data from the element of the resource (in that URL) like: <meta property="og:imag ...

Using ngIf to validate an empty string in Angular 5

I need assistance with validating an empty string retrieved from a server Although it is usually straightforward, it's just not working as expected <div class="ui-g-2 info-txt" *ngIf="appointment.Notes !==null || appointment.Notes !== ...