Is there a way to define type for array with unique items in typescript?

The only possible way this could work at compile time is if your arrays are tuples composed of literals. For example, here are some arrays with the same runtime values, but with different types in TypeScript: const tupleOfLiterals: [1, 2, 2] = [1, 2, 2]; const tupleOfNonLiterals: [number, number, number] = [1, 2, 2]; const … Read more

Typescript input onchange event.target.value

Generally event handlers should use e.currentTarget.value, e.g.: onChange = (e: React.FormEvent<HTMLInputElement>) => { const newValue = e.currentTarget.value; } You can read why it so here (Revert “Make SyntheticEvent.target generic, not SyntheticEvent.currentTarget.”). UPD: As mentioned by @roger-gusmao ChangeEvent more suitable for typing form events. onChange = (e: React.ChangeEvent<HTMLInputElement>)=> { const newValue = e.target.value; }

Types for function that applys name of function and arguments

The compiler will not be able to understand that this is type safe because it generally does not reason very well about assignability for types that depend on as-yet-unspecified generic type parameters. There is an existing GitHub issue, microsoft/TypeScript#24085, that describes this situation. In fact, it is possible (but not very likely) that in your … Read more

Import class in definition file (*d.ts)

After two years of TypeScript development, I’ve finally managed to solve this problem. Basically, TypeScript has two kind of module types declaration: “local” (normal modules) and ambient (global). The second kind allows to write global modules declaration that are merged with existing modules declaration. What are the differences between this files? d.ts files are treated … Read more