How to retrieve the value of a static property within an anonymous class in TypeScript

When a class is defined like the following in a file named MyClass.ts:

export default class {
    static someProperty = 1;

    someMethod() {
       var a = ????.someProperty
    }

}

How can we access someProperty? The code this.someProperty will not work, and neither can we use a name. If it were a named class, we could access it through SomeClassName.someProperty.

After loading the module in another file, we can access it using:

MyClass.someProperty

Answer №1

This code snippet demonstrates the usage of an anonymous class expression. It seems that in order to access the variable, naming the class expression is necessary.

 export default class ClassName {
    static someProperty = 1;

    someMethod() {
        return ClassName.someProperty;
    }

}

It's worth noting that consumers can still choose to name the class whatever they prefer (for example, MyClass as shown in your previous example).

Answer №2

Here's a way you can utilize

this.constructor.someProperty

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

Can TypeScript identify and eliminate any undefined values within an array effectively?

Is there a simple and efficient method to achieve the following: const x: (string|undefined)[] = ['aaa', undefined, 'ccc']; const y = _.filter(x, it => !!it); in order for TypeScript to correctly identify y as string[], without nee ...

Ways to retrieve the "this" keyword in a <script setup> Vue Single File Component

Currently, I am developing a basic scaffold for vite, vue, and vuetify utilizing typescript. My goal is to implement the script setup version of SFC Vue. <script setup lang="ts"> One particular challenge I am facing is how to access proper ...

The world of TypeScript generics and derived data types

I'm looking to streamline the process of calling multiple functions by creating a function that handles this task. These functions all require similar business logic and cleanup operations. function foo(arg: number) { // perform actions using arg ...

Having trouble pushing data to a GraphQL database from my Next.js application

I am currently working on developing a Reddit-like platform. To simplify the process, I have integrated SQL with graphQL using Stepzen for endpoints. Below is the code snippet of my Post Box component for the site, which includes graphQL mutations.ts and q ...

Error: Unable to access the 'dispatch' properties as it is undefined in useShoppingCart function

Currently, I am utilizing the Stripe API along with use-shopping-cart for an e-commerce platform. However, I have encountered a perplexing issue that I cannot seem to resolve. When I wrap a page with CartProvider, that specific page successfully interacts ...

Issues encountered with rest arguments while setting up gtag

I am currently working on implementing a Google Tag loading script using TypeScript. const GTAG_ID = 'ID'; const script = document.createElement('script'); script.src = `https://www.googletagmanager.com/gtag/js?id=${GTAG_ID}`; ...

Redux, React, and Typescript work together seamlessly to create a dynamic application that utilizes the Connect function to

I am currently facing an issue with the following code example: import * as React from "react"; import { connect } from 'react-redux'; interface IMessage { message : string; } class SayMessage extends React.Component<IMessage, {}>{ ...

Empowering your Angular2 application with data binding

I am currently working with the following template: <table width="700"> <caption>All Users</caption> <thead> <tr> <th>name</th> <th>surname</th> < ...

Configuring ordered imports in TSLint

Need help with configuring my TSLint rule ordered-imports. I want the import order to be like this: // React import React from 'react'; import { View } from 'react-native'; // Libs import * as _ from 'lodash'; import * as mo ...

Trouble with passing options to ES6 module imports

After coming across this Stackoverflow thread, I am attempting to pass options to ES6 imports. Initially, this code worked without any issues: export default (Param1:any, Param2:any) => { return class Foo { constructor() { cons ...

Typescript controller inheritance leading to Error: $injector:unpr Unknown Provider due to minification

LATEST UPDATE: 2019/07/16 The issue I am facing is actually a result of misusing $inject. Instead of declaring it as private $inject in api-service.ts, it should have been public static $inject = [...]. During the minification process, explicit injection ...

What makes it permissible to use `declare const foo = 3` in TypeScript?

As I was working on creating a TypeScript declaration file, I discovered that it is permissible to declare a constant and assign a value to it. declare const foo = 1; // This is considered valid declare const bar = 'b'; // This is al ...

Creating a TypeScript module declaration for exporting a class

Just starting out with TypeScript and running into issues while trying to load an es6 javascript module. Here's the code snippet from my javascript file: //TemplateFactory.js export class TemplateFactory{ static getTemplate(module){ } } In ...

Develop a TypeScript Module that consolidates all exports

My Goal with TypeScript Modules I aim to streamline my TypeScript project by creating a module that contains all the necessary class exports. Currently, I find myself using relative import statements in my classes, which can make maintenance challenging i ...

Leverage a nearby module with a local dependency

My current challenge involves integrating a local library into my project. I have been following two tutorials: how to create a library and how to consume a local library. Despite having a well-structured sample library with package.json and index.ts, I am ...

Problem with Andular's datepicker functionality and local change detection

Currently, we are facing a challenge where local change detection is necessary for our component to improve performance, but it causes issues with the mat datepicker overlay. When navigating with arrow keys within the overlay and changing the month or year ...

Specify the type of nested object exclusively with string values

I am working on creating a complex nested object structure with string values only, as shown below: let obj = { foo: "bar", foo2: "bar2", nestedFoo: { foo3: "bar3", anotherNested: { foo4: "bar4" ...

Loop through an array of strings

When I receive the data as a string[] https://i.sstatic.net/ttyag.png However, when I attempt to loop over it subUrl(arr) { for(let i of arr) { console.log(i); } } No output is being logged. What could be causing this issue? ...

Guide on integrating Amazon S3 within a NodeJS application

Currently, I am attempting to utilize Amazon S3 for uploading and downloading images and videos within my locally running NodeJS application. However, the abundance of code snippets and various credential management methods available online has left me fee ...

The concept of Nested TypeScript Map Value Type

Similar to Nested Typescript Map Type, this case involves nesting on the "value" side. Typescript Playground const mapObjectObject: Map<string, string | Map<string, string>> = new Map(Object.entries({ "a": "b", &quo ...