Getting the type from an ImmutableArray of MyType instances does not produce the desired results

In an attempt to enhance certain areas of a codebase, I came across multiple arrays utilized to convert information from our backend into user-friendly strings - similar to the structure below:

export const MyMapping = [
  { dbLabel: 'something', screenLabel: 'Something User-friendly' },
  ...
];

Due to this array being present in various locations without any specific type enforcement, I decided to create the following:

export type DbLabelMapper = Record<'dbLabel' | 'screenLabel', string>;

(initially implemented as an interface)

Since the database labels were incorrectly treated as strings instead of types in other parts of the codebase, I proceeded with the following adjustments:

export const MyMapping: Array<DbLabelMapper> = [
  { dbLabel: 'something', screenLabel: 'Something User-friendly' },
  ...
] as const;
export type MyMappingType = typeof MyMapping[number]['dbLabel'];

I encountered an error in Typescript regarding assigning a readonly type ([...] as const) to a mutable type (Array<DbLabelMapper>). Therefore, I refined the mapping signature as shown below:

export const MyMapping: Readonly = [
  { dbLabel: 'something', screenLabel: 'Something User-friendly' },
  ...
] as const;
export type MyMappingType = typeof MyMapping[number]['dbLabel'];

However, following these modifications, MyMappingType does not exhibit any defined types. Based on the desired outcome illustrated in my example, I expected something like

MyMappingType = 'something' | 'anotherThing' | '...'
. Am I overlooking a mistake here, or did I omit a crucial step?

Answer №1

When defining MyMapping, you explicitly specified its type as

Readonly<Array<DbLabelMapper>>
. By using typeof MyMapping, you can retrieve this exact type.

To resolve this, you need to remove the type annotation and allow TypeScript to infer the type automatically.

export const MyMapping = [
  { dbLabel: 'something', screenLabel: 'Something User-friendly' },
] as const

However, by doing this, you will lose the type safety that you previously had since any value can now be assigned to MyMapping.

In TypeScript 4.9, a new operator called satisfies will be introduced to address this scenario.

export const MyMapping = [
  { dbLabel: 'something', screenLabel: 'Something User-friendly' },
] as const satisfies Readonly<Array<DbLabelMapper>>

export type MyMappingType = typeof MyMapping[number]['dbLabel'];
//          ^? type MyMappingType = "something"

Playground

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

Incorporating information into the output of an HTTP request with Angular 7

I have a list of asset IDs (assetIDs) and I need to fetch data using these IDs. Each HTTP request returns one or more datasets. My goal is to include the request ID in each dataset and then return the data. The process of fetching and returning the data i ...

ngOnChanges will not be triggered if a property is set directly

I utilized the modal feature from ng-bootstrap library Within my parent component, I utilized modalService to trigger the modal, and data was passed to the modal using componentInstance. In the modal component, I attempted to retrieve the sent data using ...

Tips for typing function parameters for improved inference of return types

My goal is to define a standard function type that consistently receives the same parameter types, but allows the return type to be inferred based on the function's implementation. I've explored two approaches so far: The first approach involve ...

Setting up Jest to run in WebStorm for a Vue project with TypeScript can be done through

I am struggling to run all my tests within WebStorm. I set up a project with Babel, TypeScript, and Vue using vue-cli 3.0.0-rc3. My run configuration looks like this: https://i.stack.imgur.com/7H0x3.png Unfortunately, I encountered the following error: ...

Trouble encountered when utilizing [ngClass] - Error occurred due to changes in expression after it has been checked

I keep encountering an error when attempting to utilize [ngClass] in my Angular project. The specific error message I receive is as follows: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: ' ...

Preventing setTimeout from repeating upon reload in Angular

I have a dataset and processing code shown below: LIST = [{ "id": "1lqgDs6cZdWBL", "timeUpdated": "2020-04-22 12:51:23", "status": "CLOSED" }, { "id": "C2Zl9JWfZHSJ& ...

What is the best way to specify types for a collection of objects that all inherit from a common class?

I am new to typescript and may be asking a beginner question. In my scenario, I have an array containing objects that all extend the same class. Here is an example: class Body{ // implementation } class Rectangle extends Body{ // implementation } class ...

Using Node with Express and TypeScript to serialize date types as epoch milliseconds universally

I am looking to update my node-based API backend to serialize all Date types as Epoch milliseconds instead of ISO format. For instance, if I have the following interface: export interface Profile { updatedAt: Date; updatedBy: string; } And when I ...

What could be causing the undefined properties of my input variables in Angular?

Currently, I am fetching data from a service within the app component and passing it down to a child component using @Input. Oddly enough, when I log the data in ngOnInit, it appears correctly in the child component. However, when I try to assign it to a v ...

When a function is passed as an argument in Typescript, it may return the window object instead of the constructor

I'm still getting the hang of typescript, and I've come across a situation where a function inside a Class constructor is calling another function, but when trying to access this within sayHelloAgain(), it returns the window object instead. With ...

Encountered problem in Angular with Cognito: Unable to access property 'user' of null in authorization.service.ts at line 29

For the purpose of understanding, I am working on implementing a proof of concept involving AWS Cognito and API Gateway. I have customized the UserPoolId and ClientId in the authorization.service.ts file based on the instructions in a video tutorial. The a ...

The inRequestScope feature seems to be malfunctioning and is not functioning as intended

Need help with using inRequestScope in inversifyJS For example: container.bind<ITransactionManager>(Types.MysqlTransactionManager).to(MysqlTransactionManager).inRequestScope() ... container.get<ITransactionManager>(Types.MysqlTransactionMana ...

Is it possible to incorporate mapped types while also including a particular property?

My initial idea was something along these lines: type Foo<T, K extends string> = K extends "isDirty" ? never : { [P in K]: T; isDirty: boolean; }; However, Typescript is still unaware that K will never be `"isDirty ...

What could be causing the "Failed to compile" error to occur following the execution of npm

Exploring react with typescript, I created this simple and basic component. import React, { useEffect, useState } from "react"; import "./App.css"; type AuthUser = { name: string; email: string; }; function App() { const [user, setUser] = useState& ...

Issue TS1192: The module named "A.module" does not contain a default export

After creating a new module 'A', I attempted to import it in another module named 'B'. However, during compilation, I encountered the following error: Error TS1192: Module '" A.module"' has no default export I wou ...

Guide to testing error throwing in error events with Jest

I am having an issue with a particular learning case. The code snippet below is what I am dealing with and I aim to test error throwing: export const someFunction = async () => { //... const fileReadStream = createReadStream(absoluteFilePath) .on(&a ...

How to utilize TypeScript fetch in a TypeScript project running on node with Hardhat?

During a test in my hardhat project on VSCode, I encountered the need to retrieve the metadata object of my NFT from a specified URL. Initially, I assumed I would have to import fs to read the URL. However, while typing out the method, I impulsively opted ...

Look for identical values within a nested array

My data consists of a nested array where each element has a property called name, which can only be either A or B. I need to compare all elements and determine if they are all either A or B. Here is an example of the input: [ { "arr": { "teach ...

The .env file setting does not take precedence over the default value in the

I am facing an issue with my JSON file that contains the default convict configuration. Here is a simplified version of the file: { "env": { "doc": "The application environment.", "format": [" ...

Probability of an event occurring when represented as whole numbers in percentage form

Currently, I'm developing a unique job system within a Discord bot that allows users to mine various types of ores. The probability of receiving specific ores is based on the user's mining skill level, which is stored in a database and can vary a ...