Typescript Interface – Possible to make “one or the other” properties required?

If you’re truly after “one property or the other” and not both you can use never in the extending type: interface MessageBasics { timestamp?: number; /* more general properties here */ } interface MessageWithText extends MessageBasics { text: string; attachment?: never; } interface MessageWithAttachment extends MessageBasics { text?: never; attachment: string; } type Message = … Read more

Specify return type in TypeScript arrow function

First, consider the following notation from your original question: export const addTodo3 = (text: string) => <AddTodoAction>({ type: “ADD_TODO”, text }) Using this notation, you typecast the returned object to the type AddTodoAction. However, the function’s declared return type is still undefined (and the compiler will implicitly assume any as return type). Use the following … Read more

Create a new object from type parameter in generic class

To create a new object within generic code, you need to refer to the type by its constructor function. So instead of writing this: function activatorNotWorking<T extends IActivatable>(type: T): T { return new T(); // compile error could not find symbol T } You need to write this: function activator<T extends IActivatable>(type: { new(): T … Read more

How to fix TS2322: “could be instantiated with a different subtype of constraint ‘object'”?

Complementing @fetzz great answer. SHORT ANSWER TLDR; There are two common causes for this kind of error message. You are doing the first one (see below). Along with the text, I explain in rich detail what this error message wants to convey. CAUSE 1: In typescript, a concrete instance is not allowed to be assigned … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)