typescript-generics

Restrict generic key type based on lookup type in TypeScript

浪尽此生 提交于 2021-01-29 06:15:50
问题 I'm working on a function that takes two type parameters, T and K . T extends a Record type and K is a key of the first type. Is there a way I can restrict the key type based on its lookup type ( T[K] ) in T ? I have the following types: type FormValue = string | number | boolean | null; type FormValues = Record<string, FormValue>; and the following function: function numericFormHelperFunc<T extends FormValues, K extends keyof T>(key: K, formValues: T) {} Is there a way I can restrict which

Use function interface to ensure parameters but infer more specific return type

可紊 提交于 2021-01-29 06:13:13
问题 I have a function signature that I need a bunch of functions to follow which is something like this: type ActionCallback<R = any> = (param1: SpecificType, param2: OtherType) => Promise<R> Basically the types for the parameters are well defined and it must return a promise but what that promise resolves to is up to the function. Instead of having to specify the type of both arguments in every callback I'd like to just specify the variable conforms to ActionCallback so the parameters types are

Why does this TypeScript mixin employing generics fail to compile?

南笙酒味 提交于 2021-01-29 04:04:20
问题 I'm using mixins/traits with TypeScript using a subclass factory pattern as described at https://mariusschulz.com/blog/mixin-classes-in-typescript. The trait in question is called Identifiable , which imparts an id property to a class that should express the Identifiable trait. When I attempt to use the trait with another, non-generic trait ( Nameable ) in a certain order, compilation fails. class Empty {} type ctor<T = Empty> = new(...args: any[]) => T; function Nameable<T extends ctor =

Enforce sub-type in array of generics

那年仲夏 提交于 2021-01-28 20:13:39
问题 I'm trying to create a list of columns where each column has constraints that I would like enforced by the type system. A column renderer has a generic value property and also has a valueGetter that should return the appropriate type for the custom renderer. My difficulty is that when I declare the array, I don't know what each of the types is going to be so I'm resorting to unknown which makes my assignment to columns not enforce types. export interface Grid<R> { // How can I specify that

Type guard don't work with generic params?

本小妞迷上赌 提交于 2021-01-28 06:05:49
问题 Example: type SomeType = { foo: string; } | undefined; function someFn<TParams extends SomeType>(params1: SomeType, params2: TParams): void { if (params1) { Object.entries(params1); } if (params2) { Object.entries(params2); // here is an error } } TypeScript cannot determine that params2 does not contain undefined. Is there a logical explanation for this, or is it a mistake in TypeScript? Online demo here 回答1: This is an open issue in typescript (https://github.com/microsoft/TypeScript/issues

How to restrict the typescript T to be only camplex object

ⅰ亾dé卋堺 提交于 2021-01-28 05:42:31
问题 I have this class definition class abstract MyClass<T> { } Which can be used as class MyOtherClass extends MyClass<IInterface> { } However I want to restrict to usage of this class with all simple types: class MyOtherClass extends MyClass<string> { } //<-- do not allow or class MyOtherClass extends MyClass<nubmer> { } //<-- do not allow Basically I want to allow only T to be a complex object. Any ideas how I achieve do that? 回答1: You can extends generic type from object abstract class MyClass

Overloading class properties based on constructor arguments in typescript

吃可爱长大的小学妹 提交于 2021-01-28 00:56:56
问题 In our codebase we use a navigator and builder patterns pretty extensively to abstract away assembling hierarchical objects. At the heart of this is a Navigator class which we use to traverse different classes. I'm currently attempting to migrate this to typescript but am struggling to type it to leverage the power of typescript. I think the core of my problem is that I can't use this as the default value for a generic on a class e.g. class Something<T = this> , or that I can't overload the

Specialize a type for generic function

早过忘川 提交于 2021-01-27 21:48:47
问题 Given this definition: declare function foo<T>(): { bar: T } // <T>() => { bar: T } type Foo = typeof foo; How can one provide specialization to the generic function by its type? What I want to achieve is to be able to do something like this: // { bar: number } type FooResult = ReturnType<Foo<number>>; But TypeScript complaints that Foo itself is not generic - the function it types is. 回答1: TypeScript doesn't really support the kind of higher-order typing you'd need to get this from

How do I generically iterate over the properties of an arbitrary object in TypeScript?

对着背影说爱祢 提交于 2021-01-27 17:10:14
问题 This is a pretty common JavaScript pattern: function mapThruWrapper(module) { const replacement = {} Object.getOwnPropertyNames(module).forEach(function(key) { const val = module[key] if (val instanceof Function) { replacement[key] = wrapperFunc.bind(null, val) } else { replacement[key] = val } }) return replacement } I'm trying to strongly type this in TypeScript, and I've gotten as far as something like the following: function mapThruWrapper<M extends { [X: string]: unknown }>(module: M): M

Create a Generic Type from enum

北城余情 提交于 2021-01-27 12:55:28
问题 I'm trying to create a generic Type using enums. Enum export enum OverviewSections { ALL = 'all', SCORE = 'score_breakdown', PERFORMANCE = 'performance_over_time', ENGAGEMENT = 'engagement', COMPANY = 'company_views_graph', PEOPLE = 'people_views_graph', ARTICLES = 'articles_read_graph', PLATFORM = 'platform_sessions_graph', EMAILS = 'emails_opened_graph', } Now I'd like to create a generic type which would help me achieve something like this: Overview: { [OverviewSections.ALL]: { data: