14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-10-18

仕事で TypeScript、趣味で Swift を書いている自分用の比較表です。TypeScript は JavaScript や ES6 なども含みます。随時更新します。編集リクエスト大歓迎です。

定数・変数

Swift
let i = 0
var a = "a"
TypeScript
const i = 0;
let a = "a";

基本的な型

Swift TypeScript Note
Int number
Float number
Double number
- bigint
Bool boolean
String string
- symbol
Array Array
Dictionary Object
nil null
- undefined
Void void
Any unknown

文字列補間

Swift
let n = 10
"\(n) times." // 10 times.
TypeScript
const n = 10;
`${n} times.` // 10 times.

enum (列挙型)

Swift
enum Fruit: String {
  case Orange = "🍊"
  case Apple = "🍎"
  case Banana = "🍌"
}
print(Fruit.Orange.rawValue) // 🍊
TypeScript
enum Fruit {
  Orange = "🍊",
  Apple = "🍎",
  Banana = "🍌",
}
console.log(Fruit.Orange); // 🍊

Array(配列)

範囲外のアクセス

Swift
let i = 10
let numbers = [1, 2, 3]

print(numbers[i]) // EXC_BAD_INSTRUCTION

if i < numbers.count {
  ...
}
TypeScript
const i = 10;
const numbers = [1, 2, 3];

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

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

Swift も if let とかでチェックできるといいのだけど...

Object / Dictionary

基本

Swift
var dict: [String: Any] = [:]
dict["abc"] = 123
print(dict["abc"] ?? "No value")
TypeScript
let obj: { [_: string]: any } = {};
obj['abc'] = 123;
console.log(obj['abc'] || 'No value');

存在確認

Swift
var fruits = [
  "apple": "🍎",
  "orange": "🍊"
]
if let apple = fruits["apple"] {
  print(apple)
}

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

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

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

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

Swift
var a = ["apple": "🍎",
         "orange": "🍊"]
let b = ["apple": "🍏",
         "banana": "🍌"]
let merged = a.merging(b) { _, new in new }
// ["apple": "🍏", "banana": "🍌", "orange": "🍊"]
TypeScript
const a = {
  apple: "🍎",
  orange: "🍊"
}
const b = {
  "apple": "🍏",
  "banana": "🍌"
}
const merged = Object.assign({}, a, b)
// {apple: "🍏", orange: "🍊", banana: "🍌"}

どっちも面倒臭い。どっちも shallow マージ。

struct (構造体)

Swift
struct Person {
  var name: String
  var age: Int
};

let person = Person(name: "Taro", age: 20)
TypeScript
type Person = {
  name: string;
  age: number;
}

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

TypeScript の type を構造体とみなしていいのかどうかはわからない...

class (クラス)

Swift
class Person {
  // メンバ変数
  var word: String
  // コンストラクタ(イニシャライザ)
  init(word: String) {
    self.word = word
  }
  // デストラクタ(例はイマイチ)
  deinit {
    self.word = "Bye..."
  }
  // メソッド
  func say() -> String {
    return self.word
  }
}

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

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

TypeScript にはデストラクタはない模様。

タイマー

Swift
let timer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { timer in
  print("Fired")
}

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

clearTimeout(timer); // Stop the timer

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

14
10
1

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
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?