LoginSignup
5
4

More than 3 years have passed since last update.

TypeScript / Python 比較表 【随時更新】

Last updated at Posted at 2020-05-08

仕事で Python も書かなきゃいけなくなったので、普段書いている TypeScript との比較表。TypeScript は JavaScript や ES6 なども含みます。随時更新します。編集リクエスト大歓迎。

TypeScript / Swift 比較表(随時更新) - Qiita

定数・変数

TypeScript
let numOfPages = 0;
const pi = 3.14
Python
num_of_pages = 0;
PI = 3.14
  • Python には定数の概念がなく、大文字で表現する運用らしい。
  • Python はスネークケースが慣例のよう。

基本的な型

TypeScript Python Note
number 1 (int)
number 123456789L (long) Python2の書き方。末尾に L をつける
number 3.14 (float)
bigint 123456789 (int) Python3ではint型でbigintを扱える
boolean True / False
string "abc" / 'abc' / """abc""" / '''abc''' 3連クオートは途中で改行できて改行文字が含まれる
symbol ?
Array [1, 2, 3]
Object {'orange': '🍊' } キーのクオートは省略できない
null None
undefined ?
void ?
  • Python は明確な型宣言がなく、代入した値によって型が決まる。

文字列補間

TypeScript
const n = 10;
`${n} times.` // 10 times.
Python
n = 10
f'{n} times.' # 10 times.

enum (列挙型)

TypeScript
enum Fruit {
  Orange = "🍊",
  Apple = "🍎",
  Banana = "🍌",
}
console.log(Fruit.Orange); // 🍊
Python
from enum import Enum

class Fruit(Enum):
  Orange = "🍊"
  Apple = "🍎"
  Banana = "🍌"

print(Fruit.Orange.value) # 🍊

Array(配列)

範囲外のアクセス

TypeScript
const i = 10;
const numbers = [1, 2, 3];

console.log(numbers[i]); // undefined

if numbers[i] !== undefined {
   ...
}
Python
TBD

Object / Dictionary

基本

TypeScript
let obj: { [_: string]: any } = {};
obj['abc'] = 123;
console.log(obj['abc'] || 'No value');
Python
TBD

存在確認

TypeScript
const fruits = {
  apple: "🍎",
  orange: "🍊"
};
if ('apple' in fruits) {
  console.log(fruits['apple']);    
}

キーが存在しない場合は undefined を返す。

Python
TBD

マージ(新しい方で上書き)

TypeScript
const a = {
  apple: "🍎",
  orange: "🍊"
}
const b = {
  "apple": "🍏",
  "banana": "🍌"
}
const merged = Object.assign({}, a, b)
// {apple: "🍏", orange: "🍊", banana: "🍌"}
Python
TBD

struct (構造体)

TypeScript
type Person = {
  name: string;
  age: number;
}

const person: Person = {
  name: "Taro",
  age: 20
};
Python
TBD

class (クラス)

TypeScript
class Person {
  // メンバ変数
  word: string;
  // コンストラクタ(イニシャライザ)
  constructor(word: string) {
    this.word = word;
  }
  // メソッド
  say() {
    return this.word;
  }
}

const person = new Person("Hello!");
person.say();
Python
TBD

タイマー

TypeScript
// 繰り返しの時は、setInterval() を使う
const timer = setTimeout(() => {
  console.log("Fired");
}, 3000);

clearTimeout(timer); // Stop the timer
Python
TBD

ToDo

  • プリミティブ型
  • any / void
  • function / closure
  • getter / setter
  • キャスト
  • ループ
  • while / do while
  • if / switch
  • Array / Set
  • .count, .length
  • 比較演算子
  • &&, ||, ??
  • 三項演算子
  • 分割代入
  • スプレッド構文
  • 可変長引数
  • protocol
  • interface
  • extension
  • typeof / instanceof / in / of
  • optional
  • タプル
  • guard
  • if let = ...
  • range
  • ヒアドキュメント
  • 長さ(文字列、配列)
  • Error
  • マクロ関連
  • 環境変数
  • _
  • subscript
  • undefined
  • Never
  • union
  • ジェネリクス
  • typealias
  • some / where
  • weak / unowned
  • lazy
  • async / await
  • yield
  • public / private / etc...
  • static
  • print / console.log
  • module / namespace
  • コメントアウト

Reference

5
4
4

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
5
4