Key of union type as function argument

Advertisements I’m trying to implement a UI where a client can search using either of many attributes. AFAIK this could be achieved with union types. So far my implementation looks like this interface GivenNameSearchTerm { readonly givenName: string; } interface FamilyNameSearchTerm { readonly familyName: string; } type SearchTerm = GivenNameSearchTerm | FamilyNameSearchTerm; const search =… Read More Key of union type as function argument

How to resolve "Type is not assignable to type" in TypeScript if statement?

Advertisements I have 2 sets of keys: KEY_SET_1 and KEY_SET_2. KEY_SET_1 has keys A to D while KEY_SET_2 has keys E to H. I have a function that needs to handle key in both key sets differently. However, I run into an error that says Argument of type ‘AllKeySetType’ is not assignable to parameter of… Read More How to resolve "Type is not assignable to type" in TypeScript if statement?

How to include Angular Material in Angular 17?

Advertisements Angular Material’s documentation says: import { MatSlideToggleModule } from ‘@angular/material/slide-toggle’; @NgModule ({ imports: [ MatSlideToggleModule, ] }) class AppModule {} But Angular 17 doesn’t create app.module file by default. Is there any way to do that without app.module? >Solution : Angular v17 onwards, standalone is default via CLI Which means when you create new… Read More How to include Angular Material in Angular 17?

Why doesn't TypeScript produce an error for this mapped type?

Advertisements type JsonType<T> = { [P in keyof T as Exclude<P, "salt" | "hash">]: string; }; type Test = { salt: string; hash: string; }; const testObject: JsonType<Test> = { hash: "…", salt: "…", }; why I’m not getting any error for defining an object named testObject of type JsonType<Test>? I excluded "salt" and "hash"… Read More Why doesn't TypeScript produce an error for this mapped type?

The "error TS7030: Not all code paths return a value." caused by "strict": false in tsconfig.json

Advertisements At first, I’d like to reproduce my issue. Create a new Angular project ng new ng-ts-strict-issue cd ng-ts-strict-issue Modify compilerOptions in tsconfig.json. Let’s set strict to false. { … "compilerOptions": { … "strict": false, … }, … } Add a method to the app.component.ts. test(p: string): string | null | undefined { if (p… Read More The "error TS7030: Not all code paths return a value." caused by "strict": false in tsconfig.json

How to fix Property mutate does not exist on type WritableSignal<string[]>

Advertisements I had the following code using signals which I implemented with Angular 16.x: protected errors = signal<string[]>([]); @Input() set error(error: string) { if (error) { if (this.errors().indexOf(error) === -1) { this.errors.mutate((errors: any) => errors.push(error)); } } else { this.errors.set([]); } } After upgrading the version to Angular 17.x, I trying to run the unit… Read More How to fix Property mutate does not exist on type WritableSignal<string[]>

How to narrow discriminated union based on Record key value

Advertisements I have a union type whose elements have a discriminator property called "type". I then declare a Record<> whose keys are the "type" property values. How do I declare the Record’s second generic parameter (Record< , this param>) such that values in the map/record are inferred based on the corresponding key? TypeScript Playground interface… Read More How to narrow discriminated union based on Record key value