programming language, superset of JavaScript that compiles to JavaScript
Factsheet
Anders Hejlsberg,
Luke Hoban
Anders Hejlsberg,
Luke Hoban
Videos
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.
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.
» npm install @yarnpkg/plugin-typescript
Try npm install -g typescript@latest. You can also use npm update instead of install, without the latest modifier.
Open command prompt (cmd.exe/git bash)
Recommended:
npm install -g typescript@latest
or
yarn global add typescript@latest // if you use yarn package manager
This will install the latest typescript version if not already installed, otherwise it will update the current installation to the latest version.
And then verify which version is installed:
tsc -v
If you have typescript already installed you could also use the following command to update to latest version, but as commentators have reported and I confirm it that the following command does not update to latest (as of now [ Feb 10 '17])!
npm update -g typescript@latest
You need to build files at some point.
Within this repository yarn build (or any other build instruction if needed). You need to run it each time after you yarn upgrade package-you-import.
For a longer answer, see my answer to How to have npm install a TypeScript dependency from a GitHub URL?.
yarn add does not "compile typescript/ES6 on the fly", it just links proper folders. When yarn add-ing from github, you can always import from repo's src directory:
import * as repo from 'repo/src';