如何从 Swift 过度到 TypeScript

TypeScript具有类型系统,且是JavaScript的超集。 它可以编译成普通的JavaScript代码。 TypeScript支持任意浏览器,任意环境,任意系统并且是开源的。

同时,TypeScript 也是 CocosCreator 的推荐脚本之一。

log#

Swift:

print("Hello, world!")


TypeScript:

console.log("Hello, world!");

变量 与 常量#

Swift:

1
2
var myVariable = 42
let myConstant = 50

TypeScript:

1
2
var myVariable = 42;
const myConstant = 50;

声明类型#

Swift:

1
var myVariable: Int = 50

TypeScript:

1
var myVariable: number = 50;

类型强制转换#

Swift:

1
2
let newCount = 50
let newString = String(newCount)

TypeScript:

1
2
let newCount = 50;
let newString = String(newCount);

String 中间插入#

Swift:

1
2
let newCount = 50
let newString = "test \(newCount)"

TypeScript:

1
2
let newCount = 50;
let newString = "test" + newCount;

遍历#

Swift:

1
2
for index in 0..<5 {
}

TypeScript:

1
2
for (let index = 0 ; index < 5 ; index++){
}

Array#

Swift:

1
var list = ["1","2"]

TypeScript:

1
2
var shoppingList: string[] = ["catfish", "water",
"tulips", "blue paint"];

参考#

官方文档
swift like typescript

Swift Tips 翻译和笔记 CocosCreator 编辑器入门

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×