Category: Typescript

TypeScript: dealing with `this` component properties which are reffering neither to state, nor to props

Issue Here are my interfaces: export type alphaColorProcessing = (opacity: number) => string | string[]; export interface welcomeMapping { [key: string]: alphaColorProcessing | string; } export interface IPalette { [key: string]: string | string[] | alphaColorProcessing | welcomeMapping; } export interface IThemeContext { theme: string; palette: IPalette; setTheme?: (theme: string) => void; } export interface […]

Assign to generic function parameter in TypeScript

Issue I would like to use a generic function as an parameter. However, I can’t find how to instanciate such a type. Here’s a simple example, that I would expect to work: function foo(a: number, f: <U>(a: number) => U) { f(a) // removed the return to make the example simpler } function bar(a: number) […]

Jest: to check if a function is called by a specific instance of class in JavaScript/Typescript

Issue I am testing some express middlewares with jest. it(“should throw 400 error if request.body.id is null”, () => { const req = { body: { id: null } } as any; const res = {} as any; const next = jest.fn(); myMiddleware(req, res, next); expect(next).toBeCalledWith(expect.any(ErrorResponse)); expect(next).toBeCalledWith( expect.objectContaining({ statusCode: 400, errCode: “error-0123-2342”, message: “Field id […]

How to use TypeScript with Sequelize

Issue I already have my server application written in Node, PostgreSQL, Sequelize using Fastify. Now I would like to use TypeScript. Can anyone tell me how to begin rewriting my Server application using TypeScript. Solution Updated /** * Keep this file in sync with the code in the “Usage” section * in /docs/manual/other-topics/typescript.md * * […]

How to create a string array in the useState hook and update that same array in TypeScript?

Issue I have a React project where I am trying to create a multi select function where you can select and deselect multiple avatars at once. So far I have this: export interface IStoreRecommendationProps { title: string; description: string; discountNumber: number; discountItem: string; arr: string[]; onClickAvatar: () => void; // pass a specific parameter for […]

Typescript complains Property does not exist on type ‘JSX.IntrinsicElements’ when using React.createClass?

Issue I am using typescript to write redux application. var item = React.createClass({ render: function() { return (<div>hello world</div>) } }); export default class ItemList extends Component<any, any> { render() { return (<item />) } } Then typescript complains this: Property ‘item’ does not exist on type ‘JSX.IntrinsicElements’. Solution Your component must start with a […]

Using a for loop to update with a class method – not working

Issue I’m trying to create an array of objects with a “Node” class constructor. Each Node should have an array containing neighbouring nodes. When I use a for loop to iterate through each Node to add neighbours, it triggers the class method, but the Node object fails to update. Here’s a contrived/simplified version of what […]

Ant design sort table code not working on the react typescript

Issue I used following ant design sort table code in react type script, its not working correctly anyone know how to do that correctly My code here import { Table } from ‘antd’; import * as React from ‘react’; import { Table } from ‘antd’; const columns = [ { title: ‘Name’, dataIndex: ‘name’, filters: […]

RxJs of operator for Observable

Issue I have a function that returns Observable<void> and I need it to return an observable that immediately emits and completes. Normally I would use the of operator to do this, but it doesn’t work with void. Things I’ve tried that don’t work: return of(); // doesn’t emit return of({}); // TypeScript compilation error ‘Type […]