0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

1分でTypeScript のコードを試すためのローカル環境を作る

Last updated at Posted at 2024-07-17

2024 年 7 月時点でのメモです
Node.js の環境準備はできていることを想定しています。

インストール

ターミナル
npm init -y
npm i -D typescript ts-node-dev @types/node

設定ファイルの作成

ターミナル
npx tsc --init
npm i -D @tsconfig/recommended
tsconfig.json
{
  "extends": "@tsconfig/recommended/tsconfig.json"
}

npm scripts の登録

package.json
{
  ...
  "scripts": {
    "start": "ts-node-dev --respawn --transpile-only index.ts"
  },
  ...
}

コードを試す

index.ts
console.log('Hello World!!');
ターミナル
npm run start

番外編

ExpressでREST APIサーバーをたてる

ターミナル
npm i express
npm i -D @types/express
index.ts
import express from "express";

const PORT_NUMBER = 3000;

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post("/", (req,res) => {
  if (req.body) {
    console.dir(req.body);
    return;
  }
  console.log("error");
});

app.listen(PORT_NUMBER, () => {
  console.log(`Server is running on http://localhost:${PORT_NUMBER}`);
});

参考サイト

https://typescriptbook.jp/reference/tsconfig/tsconfig.json-settings#%E5%88%9D%E3%82%81%E3%81%A6%E3%81%AEtsconfigjson
https://github.com/tsconfig/bases
https://github.com/wclr/ts-node-dev#readme

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?