Overriding methods on built-ins based on type

Advertisements Let’s say I have a type to denote a language code, for example: type LangCodeUpper = ‘EN’ | ‘DE’ | ‘ES’; type LangCodeLower = Lowercase<LangCodeUpper>; The problem I’m trying to solve is the following: const lang: LangCodeUpper = ‘EN’; const lowerLang = lang.toLowerCase(); //this is `string`, while I want `LangCodeLower` So far, I’ve got:… Read More Overriding methods on built-ins based on type

Is there a difference between: e.g. useEffect() & React.useEffect()

Advertisements I recently came across this: React.useEffect(() => {}, []); I’m used to importing useEffect and calling it like this: useEffect(() => {}, []); which in my experience seems to be more common. Is there any difference between the two? (Maybe a performance difference?) Is there any reason to prefer one over the other? >Solution… Read More Is there a difference between: e.g. useEffect() & React.useEffect()

How to use standalone component in a non-standalone component?

Advertisements I have a standalone component which I want to use in a non-standalone component without making this component standalone as well. Is this possible? I tried various way but always get errors. I have this component import { ChangeDetectionStrategy, Component } from ‘@angular/core’; @Component({ selector: ‘app-three-d-viewer’, standalone: true, templateUrl: ‘./three-d-viewer.component.html’, styleUrls: [‘./three-d-viewer.component.css’], changeDetection: ChangeDetectionStrategy.OnPush,… Read More How to use standalone component in a non-standalone component?

type narrowing in typescript based on enum in type

Advertisements say for example I have this type in typescript: enum NumberOrStringType { NUMBER, STRING, } type NumberOrString = { dataType: NumberOrStringType, data: string | number; }; and later in the code: const numOrString: NumberOrString = { dataType: NumberOrStringType.NUMBER, data: 10 }; if (numOrString.dataType === NumberOrStringType.NUMBER) { const num: number = numOrString.data; … } else… Read More type narrowing in typescript based on enum in type

Typescript type for function calling another function, with same parameters

Advertisements I’m sure this is easy, but I don’t seem to be able to find an answer (or maybe ask the right question!) I have a library function (specifically, an i18n.translator from here: https://primitives.solidjs.community/package/i18n#static-dictionaries), which I want to wrap with another function to provide some fallback value based on other logic. const translator = i18n.translator(dict);… Read More Typescript type for function calling another function, with same parameters

What causes the Property does not exist on type error in this Angular 16 movies app?

Advertisements I have been working on an SPA with Angular 16, TypeScript and The Movie Database (TMDB). I run into a problem while working on displaying a movie’s details. In app\services\movie-service.service.ts I have: import { Injectable } from ‘@angular/core’; import { Observable } from ‘rxjs’; import { environment } from ‘../../environments/environment’; import { HttpClient }… Read More What causes the Property does not exist on type error in this Angular 16 movies app?