How to create a type excluding instance methods from a class in typescript?

I’ve found a way to exclude all properties that match a given type, thanks to this article: https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c I made a few adaptations, but here is the details: // 1 Transform the type to flag all the undesired keys as ‘never’ type FlagExcludedType<Base, Type> = { [Key in keyof Base]: Base[Key] extends Type ? never … Read more

How can I see how TypeScript computes types?

There isn’t any built-in mechanism in typescript to log out the desired info in question. However, if you are interested in understanding internal work, here’s the place in source code where the actually resolving of conditional types happens. Take a look at these places in checker.ts. ln:13258 instantiateTypeWorker() ln:12303 getConditionalType() ln:12385 getTypeFromConditionalTypeNode() ln:12772 getTypeFromTypeNode() Attached … Read more

In TypeScript, how to get the keys of an object type whose values are of a given type?

This can be done with conditional types and indexed access types, like this: type KeysMatching<T, V> = {[K in keyof T]-?: T[K] extends V ? K : never}[keyof T]; and then you pull out the keys whose properties match string like this: const key: KeysMatching<Thing, string> = ‘other’; // ERROR! // ‘”other”‘ is not assignable … Read more

Declare a component with generic type

You can also access the Type parameter through the ViewChild like this: export class Bazz { name: string; constructor(name: string) { this.name = name; } } @Component({ selector: ‘app-foo’, template: `<div>{{bazz?.name}}</div>`, exportAs: ‘appFoo’ }) export class FooComponent<T> { constructor() {} private _bazz: T; set bazz(b: T) { this._bazz = b; } get bazz(): T { … Read more

Object index key type in Typescript

You can achieve that just by using a IDictionary<TValue> { [key: string]: TValue } since numeric values will be automatically converted to string. Here is an example of usage: interface IDictionary<TValue> { [id: string]: TValue; } class Test { private dictionary: IDictionary<string>; constructor() { this.dictionary = {} this.dictionary[9] = “numeric-index”; this.dictionary[“10”] = “string-index” console.log(this.dictionary[“9”], this.dictionary[10]); … Read more

Can You Specify Multiple Type Constraints For TypeScript Generics

Typescript doesn’t offer a syntax to get multiple inheritance for generic types. However, you can achieve similar semantics by using the Union types and Intersection types. In your case, you want an intersection : interface Example<T extends MyClass & OtherClass> {} For a Union of both types : interface Example<T extends MyClass | OtherClass> {}

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