typescript-types

TypeScript: Interface cannot simultaneously extends two types

拥有回忆 提交于 2021-02-18 22:31:24
问题 I'm writing a React app using TypeScript. I use material-ui for my components. I'm writing a custom wrapper for material-ui's Button component. It looks like this: import MUIButton, { ButtonProps } from "@material-ui/core/Button"; import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles"; import classNames from "classnames"; import React, { PureComponent } from "react"; import styles from "./styles"; export interface OwnProps { color: "primary" | "danger" | "warning" |

typescript inheritance using static method

梦想的初衷 提交于 2021-02-07 20:40:41
问题 Is there a way to obtain proper type using approach described below. I have tried make makeInstance() generic but I haven't obtained Extended type. Code below. class Base { name = 'foo'; static makeInstance() { return new this(); } } class Extended extends Base { age = 10; } let base = Base.makeInstance() // is Base let extended = Extended.makeInstance(); //should be Extended console.log(base.name);//ok console.log(extended.age); //output ok; age doesn't exists console.log(extended.name);//

typescript inheritance using static method

自古美人都是妖i 提交于 2021-02-07 20:40:03
问题 Is there a way to obtain proper type using approach described below. I have tried make makeInstance() generic but I haven't obtained Extended type. Code below. class Base { name = 'foo'; static makeInstance() { return new this(); } } class Extended extends Base { age = 10; } let base = Base.makeInstance() // is Base let extended = Extended.makeInstance(); //should be Extended console.log(base.name);//ok console.log(extended.age); //output ok; age doesn't exists console.log(extended.name);//

Types for function that applys name of function and arguments

折月煮酒 提交于 2021-01-30 09:07:11
问题 I'm trying to type in proper way function that applys name of function and argumens for this function. After that apply it and return the result. Here the code: const sum = (a: number, b: number) => a + b const concat = (a: string, b: string, c: string) => a + b + c const funs = { sum, concat } type Keys = 'sum' | 'concat' type Args<T> = T extends (...args: infer R) => any ? R : never type Sum = Args<typeof sum> type Concat = Args<typeof concat> function apply<K extends Keys>(funKey: K, ..

Types for function that applys name of function and arguments

心不动则不痛 提交于 2021-01-30 09:04:20
问题 I'm trying to type in proper way function that applys name of function and argumens for this function. After that apply it and return the result. Here the code: const sum = (a: number, b: number) => a + b const concat = (a: string, b: string, c: string) => a + b + c const funs = { sum, concat } type Keys = 'sum' | 'concat' type Args<T> = T extends (...args: infer R) => any ? R : never type Sum = Args<typeof sum> type Concat = Args<typeof concat> function apply<K extends Keys>(funKey: K, ..

TypeScript and React: How to overload a higher order component / decorator?

折月煮酒 提交于 2021-01-29 08:01:05
问题 I'm building a React app using TypeScript. I'm writing a higher order component and I would like to overload it's arguments. How can I do that? Let me give you what I've tried so that you can understand it better: const myHOC = ((value: string, anotherValue: number) | completeProps: SomeProps) => (WrappedComponent: ComponentClass) => // ... HOC code So it should be either: const myHOC = (value: string, anotherValue: number) => (WrappedComponent: ComponentClass) => // ... or const myHOC =

Initializing generic type inside generic class in Typescript

对着背影说爱祢 提交于 2020-01-06 08:09:09
问题 I want to initialize a generic class variable inside a generic class using a generic type, but I can't figure out if there is a way to do this. Initializing with types works fine, it doesn't seem like it work with generics though. type EventCallback<I, O> = (event: I) => O; type ListenerList<K extends string | symbol | number, I, O, V extends EventCallback<I, O>> = { [T in K]: V[]; }; const test: ListenerList<string, string, string, (event: any) => any> = {}; // Works fine export default

How to declare a type in TypeScript that only includes objects and not functions

放肆的年华 提交于 2019-12-24 06:38:54
问题 Is it in TypeScript somehow possible to define a type so that it only includes objects but not functions? Example: type T = { [name: string]: any } // How to modify this to only accepts objects??? const t: T = () => {} // <- This should not work after modification Thanks for your help. 回答1: There's no perfect way to refer to a type like "an object which is not a function", since that would require true subtraction types, and that doesn't exist in TypeScript (as of 3.1 at least). One easy

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

时间秒杀一切 提交于 2019-12-23 00:39:00
问题 The type should detect if the array has duplicate items and throw error in typescript? type UniqueArray = [ // How to implement this? ] const a:UniqueArray = [1, 2, 3] // success const b:UniqueArray = [1, 2, 2] // error PS: I'am currently removing duplicate items using JS, but, curious if this error can be captured using typescript type before hand? 回答1: 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

TypeScript: Either or for functions passed down through several components in React

空扰寡人 提交于 2019-12-13 03:40:08
问题 I'm writing a React Native application using TypeScript. I have a component EmotionsRater that accepts one of two types: Emotion or Need. It should also either accept a function of type rateNeed or rateEmotion . I combined these types to one called rateBoth using the | operator. And it passes this combined type down to another component called EmotionsRaterItem . The problem is that EmotionsRaterItem then claims: Cannot invoke an expression whose type lacks a call signature. Type 'rateBoth'