"Unresolved Class / Interface Error: The variable 's' is not defined

Exploring Typescript and Angular2 for the first time has been a learning experience, especially when it comes to classes and interfaces.

Take a look at my interface:

export interface Discount {
    codSco: string;
    desSco: string;
}

Now I'm attempting to create an instance of type Discount in the following routine:

var Discount;

s.codSco = '44';
s.desSco = '44';

Unfortunately, this results in the error message:

TypeError: s is undefined

I'm thinking maybe I need to build a class that inherits from the interfaces. Can you offer some guidance on how to do this?

Thank you

Answer №1

Choice 1:

export interface Promotion {
    code: string;
    description: string;
}

const promo: Promotion = {
  code: 'ABC',
  description: '50% off'
};

Choice 2:

export class Promotion {
    code: string;
    description: string;
}

const promo = new Promotion();

promo.code = 'ABC';
promo.description = '50% off';

In conclusion: Always remember to assign values to the variables you define, whether using a constructor or object literals.

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

Create a constant object interface definition

Is there a way to create an interface for an object defined with the as const syntax? The Events type does not work as intended and causes issues with enforcement. const events = { // how can I define an interface for the events object? test: payload ...

"encountered net::ERR_NAME_NOT_RESOLVED error when trying to upload image to s3 storage

I am currently developing an application using Angular. I have been attempting to upload a picture to my S3 bucket, but each time I try, I encounter this error in the console. https://i.stack.imgur.com/qn3AD.png Below is the code snippet from my upload.s ...

Explore a recursive JSON format to identify matching numerical values in a Set using Typescript in Angular

My project involves an initial UI step where users need to check checkboxes with sequential IDs. The JSON structure for this task is outlined below: { "categories": [{ "name": "Product", "labels": [{ "id": 1, "name": "I work on an as ...

Struggling with using Redux with tassign in Angular (typescript) to combine state.array and action.array. However, encountering an issue where state.array.join is not a function

Redux function to combine all videos: function combineAllVideos(state, action) { return tassign(state, { allVideos: state.allVideos.concat([action.data]) }); } Declaration + State for all videos array: allVideos: Array<Object>; OR allVid ...

Updating the value in React context does not result in the value being updated

I am in the process of developing a simple routing system where users can either be authenticated or not. I have been using hooks to implement this functionality, but so far, it has not been as successful as I hoped for. authProvider.tsx import React, {Di ...

Having trouble getting POST requests to work in Angular 8 for unknown reasons

Hey there! I'm currently experimenting with using POST requests to an API in Angular for the first time, but unfortunately it's not working as expected. I've checked out some other examples of code and everything seems fine. Here is a snipp ...

Obtaining radio button values from user input forms in Angular

I need help with retrieving values from a user input form. The first option is a select dropdown, the third one is a time picker which are both working fine. However, I'm struggling with the second option, which is a radio button group with two button ...

Error: The value of "$tweetId" cannot be parsed as it is set to "undefined". Please ensure that string values are properly enclosed

I am utilizing sanity, and if you require more details, I will furnish it promptly. When I try to access http://localhost:3000/api/getComments, I encounter the following error message: ClientError: Unable to process value of "$tweetId=undefined". Kindly ...

Tips for creating a sequelize transaction in TypeScript

I am currently working with sequelize, node js, and TypeScript. I am looking to convert the following command into TypeScript. return sequelize.transaction().then(function (t) { return User.create({ firstName: 'Homer', lastName: ' ...

Sorting in an Angular mat table can be customized to consider two properties within a single

Trying to implement a table with MatSort, I've encountered an issue where every column sorts except for the third one: <ng-container matColumnDef="nbreEnregistrementTotal"> <th mat-header-cell *matHeaderCellDef mat-sort-header ...

Angular is throwing an error with code TS2322 stating that a number cannot be assigned to a string type

While trying to convert a PHP variable gender from string to number, I ran into the following error: Error TS2322: Type 'number' is not assignable to type 'string' in Angular Here's the code snippet: param = { id: '&ap ...

Perform validation on input by monitoring checkbox changes in Angular 4

I am currently working on a project where I need to loop through an array of key values in order to create a list of checkboxes, each accompanied by a disabled input field as a sibling. The requirement is that upon checking a checkbox, the corresponding in ...

Trouble with updating a variable within a loop in Cypress

During my experience with writing Cypress tests, I came across an issue that is preventing me from updating a specific variable. The goal of my test is to run a loop and update the questionId variable within each iteration for making API queries. However, ...

What is the solution to fixing a 400 bad request error in Angular Routing?

Encountering an issue on a particular route where a 400 error is displayed in the screenshot every now and then. It seems to work fine for a few minutes after deleting cookies, but the error resurfaces after accessing it multiple times. Other routes are fu ...

Error: "the cart variable in the ctx object has not been defined"

A project I'm currently working on involves a pizza ordering app, and my current focus is on implementing the Cart feature. Each user has their own cart, which includes specific details outlined in cart.ts import { CartItem } from './cartitem&a ...

Guide to effortlessly loading files with designated decorators in a node.js application

Within the realm of Project A, I have constructed a decorator and am seeking a method to automatically load all these decorators upon initializing the application in Project B, which is the project utilizing Project A. Is there a way to accomplish this tas ...

Fulfill the promise once the callback has been triggered

In my code, I am working on preventing the method _saveAddress from being executed multiple times. To achieve this, I have created a promise for the method. const [pressEventDisabled, setPressEventDisabled] = useState(false); <TouchableOpacity style={s ...

When trying to gather multiple parameters using @Param in a NestJS controller, the retrieved values turn out

Can someone help me understand why I am struggling to retrieve parameters using the @Param() decorators in my NestJS controller? These decorators are defined in both the @Controller() decorator argument and the @Get() argument. I am relatively new to Nest ...

What steps can I take to resolve the problem of my NativeScript app not running on android?

When I entered "tns run android" in the terminal, the default emulator API23 launched but my app didn't install. Instead, an error occurred. This is different from when I run it on the IOS simulator, which runs smoothly without any errors. The nati ...

Unable to showcase the content inside the input box

I am having trouble displaying a default value in an input field. Here is how I attempted to do it: <input matInput formControlName="name" value="Ray"> Unfortunately, the value is not appearing as expected. You can view my code o ...