Relative Content

Tag Archive for typescripttypes

Typescript JSON.parse() to class

I am experimenting with typescript in the typescript playground and found that it allows me to assign the object returned by JSON.parse() to a class, even though the object won’t have any methods, and this causes a runtime error. This is surprising to me as my understanding was that typescript is typesafe as long as you don’t use as or Any.

TypeScript Exhaustive Type Checking for Dynamic Form Fields Not Working as Expected

I am working on a TypeScript solution to ensure exhaustive type-checking for dynamic form fields. The goal is to make sure that all required fields defined in an interface are included in the form configuration (I’m 90% there). While the first example works as expected, the second example should throw a type error because it is missing a required field, but no error is thrown. Here is a TsPlay link or my code below

Is there a way to make mapped type which takes optional or array values only

I have type:
type Primitive = number | string | boolean | object | symbol;
and a function which I want to be sure gets objects which have all properties of type Primitive | undefined or Primitive[] but which can’t have properties of type Primitive or undefined or null etc.
I tried type OptionalPrimitive = (number | undefined) | (string | undefined) | (boolean | undefined) | (object | undefined) | (symbol | undefined);
and later using it in mapped type:
[key: string]: OptionalPrimitive | Primitive[]; but it doesn’t work

typescript union type judgment is not intuitive

type Has2<T extends readonly any[], U> = T[number] extends U ? true : never; type UU = [‘Kars’, ‘Esidisi’]; type AA = Has2<UU, ‘Kars’>; I think AA should be type true, but type is never I don`t know why i try to ask chatGPT but it`s answer is that type AA is true typescript types […]