typescript-typings

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" |

Set global declaration in vscode JavaScript

僤鯓⒐⒋嵵緔 提交于 2021-02-18 20:52:08
问题 I'm working on a JavaScript transpiler that apart from other things will also replace certain functions and variables upon build. For example the following file ( ./src/my-module.js ): defineModule("MyModule", function(exports) { return exports; }); Will be copied and converted to ( ./build/my-module.js ): (function(global, factory) { "use strict"; if (typeof exports !== "undefined" && typeof module !== "undefined") module.exports.MyModule = factory(exports.MyModule || {}); else factory

Set global declaration in vscode JavaScript

时光总嘲笑我的痴心妄想 提交于 2021-02-18 20:52:05
问题 I'm working on a JavaScript transpiler that apart from other things will also replace certain functions and variables upon build. For example the following file ( ./src/my-module.js ): defineModule("MyModule", function(exports) { return exports; }); Will be copied and converted to ( ./build/my-module.js ): (function(global, factory) { "use strict"; if (typeof exports !== "undefined" && typeof module !== "undefined") module.exports.MyModule = factory(exports.MyModule || {}); else factory

TypeScript: tuple type to object type

谁都会走 提交于 2021-02-18 08:23:27
问题 Consider a tuple like this: type MyTuple = [A, B]; where A and B both have an attribute named key . For instance, interface A = { key: 'sandwiches' } interface B = { key: 'pasta' } I desire the following interface: interface Result { sandwiches: A; pasta: B; } Is there a way to do this dynamically? I'm thinking that, if this is achievable, it might look something like: type MapTuple<T> = { [K in keyof T]: T[K]["key"] } but this doesn't work. This question is the inverse of Typescript: object

TypeScript Express Error Function

孤人 提交于 2021-02-16 16:26:13
问题 What are the types of the four parameters for the error handling function when coding in Typescript? app.use((err: ??, req: ??, res: ??, next: ??) => { }); I am using VS Code and no hints are offered. I get red wiggly lines under all four parameters. The error says "Parameter implicitly has 'any' type". Actually I am confused with this message. If it treats it as an any type, then isn't that a valid choice? 回答1: The function itself has the following signature (taken from DefinitelyTyped):

Extending a class unknown at transpile time with correct typescript typings

谁说胖子不能爱 提交于 2021-02-11 12:47:56
问题 I want to extend a class which does not exist at transpile time. It is only loaded runtime (global library). However, we got the type definitions ofthe library. Is there any other way than using any for the base class which doesn't have the advantages of typing? Example with following error message: Type 'A' is not a constructor function type. /* Some external library with typings. */ class A { constructor() { } } /* Factory to inject dependency which does not exist at transpile time, only at

Typescript: Property type wildcard

不想你离开。 提交于 2021-02-11 06:42:33
问题 I have a given interface which comes from another package, so I can't really change it. For the sake of simplicity, let's just say the following interface: interface SomeInterface { someProp: string; someOtherProp?: number; } I was wondering if I can somehow extract the types of all properties so I would get a combined type which could be any of the original Interface's properties. In the example, it would resolve to a type being string | number | undefined . Also, not really the same

Unordered tuple type

假装没事ソ 提交于 2021-02-10 14:46:34
问题 I am just digging into Typescript typings and I wondered how to define a type which is a tuple but with unordered element types. I mean, having type SimpleTuple = [number, string]; const tup1: SimpleTuple = [7, `7`]; // Valid const tup2: SimpleTuple = [`7`, 7]; // 'string' is not assignable to 'number' // and vice-versa This is useful in many cases, but what if I don't care about order or I need it to be unordered. The example above is quite trivial since I could define type

Unordered tuple type

本小妞迷上赌 提交于 2021-02-10 14:45:06
问题 I am just digging into Typescript typings and I wondered how to define a type which is a tuple but with unordered element types. I mean, having type SimpleTuple = [number, string]; const tup1: SimpleTuple = [7, `7`]; // Valid const tup2: SimpleTuple = [`7`, 7]; // 'string' is not assignable to 'number' // and vice-versa This is useful in many cases, but what if I don't care about order or I need it to be unordered. The example above is quite trivial since I could define type

redeclare incorrect typsescript type in 3rd party library

喜欢而已 提交于 2021-02-10 05:20:22
问题 I'm using winston 3.0 with the @types/winston types. These types are not yet fully compatible, and I've come across an error in the types which I don't know how to correct. Here's my code. logger.ts export function middleware(): express.Handler { const transport = new winston.transports.Console({ json: true, colorize: true, stringify: getStringify() }); const loggerOptions: expressWinston.LoggerOptionsWithTransports = { transports: [transport], meta: true, msg: "HTTP {{req.method}} {{req.url}