Categories
Mastering Development

TypeScript not finding variable declared in type declaration if I import a file

I made a very simple library which contains a .d.ts (source) which has the following structure (I omit the details because the file works fine, it likely is a config issue in my project): // Some helper types and interfaces type TranslationFunction = /* … */; export { TranslationFunction }; Then, I do npm i […]

Categories
Development

Why is this React Typescript app throwing errors?

I just created a new React app using npx create-react-app my-app –template typescript and it was working fine until I changed my component to from a stateless one to a stateful one. The error: JSX element type ‘App’ is not a constructor function for JSX elements. TS2605 3 | import App from “./App”; 4 | […]

Categories
Development

Typescript cannot find module defined in paths

I have a typescript application that has paths for absolute imports. My tsconfig looks like this: { “compilerOptions”: { “baseUrl”: “.”, “paths”: { “utils*”: [“src/utils*”], “operations*”: [“src/operations*”], “actions*”: [“src/actions*”], “components*”: [“src/components*”], “constants*”: [“src/constants*”], }, “outDir”: “dist”, “module”: “esnext”, “target”: “esnext”, “moduleResolution”: “node”, “noEmit”: true, “lib”: [“es2017”, “dom”], “noUnusedLocals”: true, “sourceMap”: true, “skipLibCheck”: true, “allowJs”: true, […]

Categories
Development

Nuxt TypeScript error: nuxt:typescript Cannot find module ‘@/my-module’

I have setup Nuxt with TypeScript using the instructions from https://typescript.nuxt.org. The transition was pretty seamless and ok, except that I can’t get rid of this error that nuxt:typescript “Cannot find module”, which obviously is a false-positive. My imports look like this: import InputField from ‘@/components/InputField.vue’ I have tried with ~/ and without the .vue […]

Categories
Development

Jest & React & Typescript & React-Testing-Library error

I have a React/NextJS project setup using Typescript and am adding unit testing with Jest and React Testing Library. A unit test for my component looks like this: import React from ‘react’; import ‘@testing-library/jest-dom/extend-expect’; import { render } from ‘@testing-library/react’; import AppLayout from ‘.’; describe(‘<AppLayout>’, () => { it(‘renders children’, () => { const children […]

Categories
Development

“Navbar refers to a value, but is being used as a type here” when trying to render a shallow copy of my component when testing

I am trying to write a test to my React component, using TypeScript, Jest as my test runner and Enzyme for testing my React components. Whenever I pass my component into the shallow Enzyme function, I get the ts error “‘Navbar’ refers to a value, but is being used as a type here.”, and underneath […]

Categories
Development

How to correctly type the apply method for browser console logging functions?

I’ve written the below class to capture the console.log function in my app so I can review the logs on devices where I can’t easily access the browser console and so later I can bundle the captured logs when reporting errors. In order to get it working, I’ve had to add ts-ignore like this: // […]

Categories
Development

WebStorm breakpoints issue with TypeScript React App (using webpack)

I tried to configure the JavaScript Debugger as the WebStorm website is telling so: https://blog.jetbrains.com/webstorm/2017/01/debugging-react-apps/ However, it worked like for 2 minutes and it doesn’t work anymore, I can’t tell why.. Is it at least stable with TypeScript? Can I use the JavaScript Debugger? Edit 1 (based on @lena comment) Here you can find my […]