Curiosity in my 50s. Engineering, AI, English, and lifelong learning.
by Masao
現代のフロントエンドの典型的な役割分担
Frontend
================
HTML
CSS
JavaScript
TypeScript
React
Backend
================
Python
Java
Go
Node.js
.NET
Data / AI
================
Python
SQL
LLM
Component
├── Structure
│ JSX
├── Logic
│ TypeScript
└── Style
CSS
SwiftUIとReactの比較 | | SwiftUI | React | |-|-|-| | macOSネイティブ感 | ◎ | △ | | Web共有 | △ | ◎ | | クロスプラットフォーム | △ | ◎ | | Apple API連携 | ◎ | △ | | UI開発速度 | ○ | ◎ |
# JavaScriptの実行環境のversion check
node --version
# TypeScriptをJavaScriptに変換するコンパイラのversion check
tsc --version
# Globa/Projectに導入されているか確認
which tsc
npm list -q --depth=0
# npm 初期化
npm init -y
# typescriptをプロジェクト単位でインストール
npm install --save-dev typescript
# typescriptのversion check
npx tsc --version
# もしグローバルにインストールする場合
npm install -g typescript
TypeScript コンパイル → 実行の流れ
# 毎回、hello.jsを削除するのは面倒なため、tsconfig.jsonを作成し、dist/にhello.jsを出力
# tsconfig.jsonを作成
npx tsc --init
Created a new tsconfig.json
#tsconfig.jsonを編集
{
"compilerOptions": {
"target": "...",
"module": "...",
"outDir": "./dist"
}
}
この設定のあとは、npx tscでコンパイル
npx tsc tsconfig.jsonの設定通りにプロジェクト全体をコンパイルしてという意味
node dist/hello.js これで実行
// 現代TypeScriptでは基本constを使う
// 値が変化する場合だけletを使う
// 内部変数は型推論に任せることが多い
const msg: string = "Hello Alfred!!!"
const message = "Hello TypeScript"
const age = 50
// 型を明示、letは後代入可能
let category: string
if (age >= 40) {
category = "Senior"
} else {
category ="Young"
}
// 現代的な書き方
// const category = age >= 40 ? "Senior" : "Young"
// const age: number = "50" ## わざと間違える
// consoleの表示機能で表示
console.log(msg)
console.log(message)
console.log(age)
console.log(category)
// 関数の定義
function greet(name: string) {
return `Hello ${name}`
}
const result = greet("Smith")
console.log(result)
// オブジェクト (interface)
interface User {
name: string
age: number
role: string
}
const user: User = {
name: "Masao",
age: 50,
role: "Software Designer"
}
console.log(user)
console.log(user.role)
// interface + function
function showUser(user: User) {
return `${user.name} (${user.role})`
}
const userInfo = showUser(user)
console.log(userInfo)
// interface, 配列, 型, arrow function, forEach
TypeScript Coding Lab #001
環境
├── Node.js
├── npm
├── package.json
├── node_modules
├── tsconfig.json
言語基礎
├── const
├── let
├── 型注釈
├── 型推論
実行モデル
├── .tsを書く
├── tscでコンパイル
├── .js生成
├── nodeで実行
プログラム構造
├── function
├── parameter
├── return
データモデル
├── object
├── interface
├── type contract