programming language, superset of JavaScript that compiles to JavaScript

TypeScript (TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications. It transpiles to JavaScript. It is developed by … Wikipedia
Factsheet
Family ECMAScript
Designed by Microsoft,
Anders Hejlsberg,
Luke Hoban
Factsheet
Family ECMAScript
Designed by Microsoft,
Anders Hejlsberg,
Luke Hoban
🌐
TypeScript
typescriptlang.org › download
TypeScript: How to set up TypeScript
Then you use a dependency manager like npm, yarn or pnpm to download TypeScript into your project. ... All of these dependency managers support lockfiles, ensuring that everyone on your team is using the same version of the language. You can then run the TypeScript compiler using one of the ...
🌐
Stack Overflow
stackoverflow.com › questions › 69051507 › win10-yarn-cant-find-typescript
win10 yarn cant find typescript
You can just simply install in your project by using yarn add typescript @types/node @types/react @types/react-dom @types/jest or npm install --save typescript @types/node @types/react @types/react-dom @types/jest.
🌐
Yarn
classic.yarnpkg.com › en › package › typescript
Yarn
For Yarn 2+ docs and migration guide, see yarnpkg.com.
🌐
WebDevAssist
webdevassist.com › typescript › typescript-installation
How to install TypeScript
If you have installed any package manager like npm, yarn or pnpm, you can install typescript with just one line: ... We can also install typescript through VSCode while using NuGet. You can either open Manage Nuget Package window or Nuget package manager console with the command Install-Package Microsoft.TypeScript.MSBuild to install TypeScript.
🌐
Plain English
plainenglish.io › blog › getting-started-with-yarn-3-and-typescript-125e7b537e6c
Getting Started with Yarn 3 and TypeScript
To get started you'll want to import the TypeScript plugin for Yarn, and install TypeScript itself of course, along with getting the environment setup using @yarnpkg/sdks we learned about earlier.
🌐
Medium
medium.com › @tharindugimras › typescript-setup-with-nodejs-yarn-package-manager-nodemon-fa9fb2275655
Typescript setup with Nodejs + Yarn Package manager + Nodemon | by Tharindu Gimras | Medium
March 3, 2023 - In this project, you can see that I’m using yarn as my package manager. To initialize the configuration file for the yarn package manager, let’s run these commands in your terminal/command line at typescript_with_node/ path.
Top answer
1 of 2
1

As my comment mentions in the original question, from the yarn documentation:

TypeScript uses its own resolver as well. In this case the situation is a bit more complex - the TS team has some concerns about allowing third-party hooks inside the tsc compiler, meaning that we can’t work with it at the moment. That being said, TypeScript isn’t only tsc and as such we’ve been able to add PnP support to the popular ts-loader - meaning that as long as you compile your TypeScript through Webpack, everything works well! Consult the dedicated section about it for more information.

To enable yarn package resolution when using Typescript you have to use webpack with ts-loader. This is how I resolved it.

  • install the neccessary dependencies:

npm install webpack webpack-cli pnp-webpack-plugin ts-loader

  • Create a 'webpack.config.js' file in your project root directory with the following content:
const path = require('path');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

const commonConfig = {
  mode: 'development',
  devtool: 'source-map',
  plugins: [
    new NodePolyfillPlugin(),
  ],
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
  },
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    plugins: [PnpWebpackPlugin],
  },
  resolveLoader: {
    plugins: [PnpWebpackPlugin.moduleLoader(module)],
  },
};

module.exports = [
  Object.assign(
    {
      target: 'electron-main',
      entry: { main: './src/main.ts' },
    },
    commonConfig
  ),
  Object.assign(
    {
      target: 'electron-renderer',
      entry: { preload: './src/preload.ts' },
    },
    commonConfig
  ),
];
  • I updated my package.json with scripts to run webpack:
{
  "name": "ElectroMega",
  "packageManager": "[email protected]",
  "private": true,
  "scripts": {
    "build": "webpack",
    "prestart": "yarn run build",
    "start": "electron ./dist/main.js"
  },
  "devDependencies": {
    "html-webpack-plugin": "^5.3.2",
    "node-polyfill-webpack-plugin": "^1.1.4",
    "pnp-webpack-plugin": "^1.7.0",
    "ts-loader": "^9.2.6",
    "typescript": "^4.4.3",
    "webpack": "^5.54.0",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {
    "@tsconfig/node14": "^1.0.1",
    "@types/node": "^16.9.6",
    "electron": "^14.0.1"
  }
}
  • Then you should be able to build your code with the command:

yarn build

My solution was a result of following the webpack GettingStarted section that explained much of the troubles I had, and the basics of using webpack.

2 of 2
0

FWIW, iamkneel's answer from above worked for my. Had an express app that I was using Typescript for and set it up with yarn. Setting nodeLinker: node-modules in the .yarnrc.yml file took care of the compile issue.

🌐
Medium
losikov.medium.com › part-1-project-initial-setup-typescript-node-js-31ba3aa7fbf1
Part 1. Project initial setup: TypeScript + Node.js | by Alex Losikov | Medium
November 3, 2020 - Install initial dependencies (-D flag is a development dependency): $ yarn add @types/node typescript $ yarn add -D ts-node
Find elsewhere
🌐
Yarn
yarnpkg.com › package
Yarn
Yarn guarantees that installs that work today will keep working the same way in the future.
🌐
Medium
medium.com › swlh › getting-started-with-yarn-2-and-typescript-43321a3acdee
Getting started with Yarn 2 and TypeScript | by Sinclair | The Startup | Medium
October 26, 2021 - ➤ YN0000: └ Completed in 0.22s ➤ YN0000: ┌ Link step ➤ YN0000: └ Completed ➤ YN0000: Done in 0.54s PS C:\…> yarn plugin import typescript ➤ YN0000: Downloading https://.../plugin-typescript.js ➤ YN0000: Saving the new plugin in .yarn/plugins/@yarnpkg/plugin-typescript.cjs ➤ YN0000: Done in 0.67sDid I mention Yarn Modern is fast? This just installs typescript, of course, but also adds the TypeScript plugin!
🌐
GitHub
github.com › yarnpkg › yarn › issues › 2154
yarn installs incorrect version for typescript, because dist-tag is ignored · Issue #2154 · yarnpkg/yarn
September 3, 2016 - # yarn lockfile v1 typescript@^2.0.10: version "2.0.10" resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.0.10.tgz#ccdd4ed86fd5550a407101a0814012e1b3fac3dd" Now remove the yarn.lock and run $ yarn install.
Published   Dec 05, 2016
🌐
Create React App
create-react-app.dev › docs › adding-typescript
Adding TypeScript | Create React App
npx create-react-app my-app --template typescript Copy ... If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global ...
🌐
DEV Community
dev.to › aryclenio › how-to-setup-a-nodejs-project-with-typescript-4ehi
How to setup a NodeJS project with Typescript - DEV Community
July 19, 2020 - In this post, you will learn how ... to Typescript and take advantage of the features of a typed language. The first task to follow is the initialization of the npm project, which can be created by npm itself as well as by Yarn, which is highly recommended for its speed of installation and the possibility of sharing dependencies between projects. You can install Yarn on the official website with versions for Windows, Linux and ...
🌐
GitHub
github.com › yarnpkg › yarn › issues › 4226
Installing Typescript definitions for react and react-addons-test-utils is inconsistent with npm · Issue #4226 · yarnpkg/yarn
August 22, 2017 - You switched accounts on another tab or window. Reload to refresh your session. ... Installing Typescript definitions for react and react-addons-test-utils is inconsistent with npm#4226 ... What is the current behavior? Performing a yarn add of @types/react@^15.6.1 and @types/react-addons-test-utils creates output that is inconsistent with npm install of the same packages.
Published   Aug 22, 2017
🌐
GitHub
github.com › yarnpkg › berry › issues › 2935
[Bug] Cannot install typescript 4.3.2 with yarn 2 / node_modules linker · Issue #2935 · yarnpkg/berry
April 13, 2021 - { "version": "0.59.0", "devDependencies": { "typescript": "^4.3.0" } } Environment if relevant (please complete the following information): OS: Windows 10 · Node version 16.2.0 · Yarn version 2.4.1 · 👍React with 👍29yurtaev, gforge, knakayama, crazy-max, gluons and 24 more❤️React with ❤️5enumiton, rgomezcasas, gluons, eugene-kim and teux ·
Published   May 26, 2021