ð ããã°ã©ãã³ã°TypeScript
æ¬æžã®ç·Žç¿çšGithubãªããžããª
1ç« ã€ã³ãããã¯ã·ã§ã³
æŽã«è©³ããèŠãã«ã¯...
TypeScriptã®å Žåã ãš
ãããã¹ããšãã£ã¿ãŒã§å ¥åããæãã«ãšã©ãŒã¡ãã»ãŒãžãäžããŠãããã
3 + []
// ãšã©ãŒ TS2365: æŒç®å '+' ãå '3' ããã³ 'never[]' ã«
// é©çšããããšãã§ããŸããã
2ç« TypeScript : å šäœå
ããã°ã©ã ãã³ã³ãã€ã©ãŒã«ãã£ãŠè§£æããããŸã§
- ããã°ã©ã ãASTãžãšè§£æããã
ASTïŒæœè±¡æ§ææšïŒabstract syntax treeïŒïŒãšã¯ã空çœé¡æåãã³ã¡ã³ãã®ãããªãã®ãç¡èŠããããŒã¿æ§é ã®ããš
ãã£ãšè©³ãã...
éåžžã®æ§ææšïŒå ·è±¡æ§ææšãããã¯è§£ææšãšãèšãïŒãããèšèªã®æå³ã«é¢ä¿ãªãæ å ±ãåãé€ããæå³ã«é¢ä¿ããæ å ±ã®ã¿ãåãåºããïŒæœè±¡ããïŒæšæ§é ã®ããªãŒã®ããš
åŒçš : Wikipedia
- ASTããã€ãã³ãŒãã«ã³ã³ãã€ã«ããã
ãã€ãã³ãŒããšã¯ããœãŒã¹ã³ãŒãããã³ã³ãã¥ãŒã¿ãå¹çããããã°ã©ã ãå®è¡ãã圢ã«ãããã®
- ãã€ãã³ãŒããã©ã³ã¿ã€ã ã«ãã£ãŠè©äŸ¡ããã
ã©ã³ã¿ã€ã ãšã¯ããå®è¡æãã®ããšã§ãããã°ã©ã ãå®è¡ãããŠããç¶æ ã®ããš
æŽã«è©³ãã...
ãã¹ãŠã®ããã°ã©ãã³ã°èšèªã«ãããŠå®è¡ã¢ãã«ãèŠå®ãããŠãããå€ãã®èšèªã§ãã®ã¢ãã«ã®å°ãªããšãäžéšãã©ã³ã¿ã€ã ã·ã¹ãã ã«å®è£ ãããŠãã
TypeScriptã®ã³ã³ãã€ã«ããã»ã¹
TypeScript
- TypeScriptãœãŒã¹ â TypeScript AST
- ASTãåãã§ãã«ãŒã«ãã£ãŠãã§ãã¯ããã
- TypeScript AST â JavaScriptãœãŒã¹
JavaScript
4. JavaScriptãœãŒã¹ â JavaScript AST
5. AST â ãã€ãã³ãŒã
6. ãã€ãã³ãŒããã©ã³ã¿ã€ã ã«ãã£ãŠè©äŸ¡ããã
2ç« - 2
åã·ã¹ãã
åã·ã¹ãã ãšã¯ãããã°ã©ããŒãäœæããããã°ã©ã ã«åãå²ãåœãŠãããã«åãã§ãã«ãŒã䜿çšããã«ãŒã«ã®éãŸã
2çš®é¡ããã
- æ瀺çãªæ§æã§åãã³ã³ãã€ã©ãŒã«äŒããïŒã¢ãããŒã·ã§ã³ïŒ
- åãèªåçã«æšè«ãããã®
ãã¬ãŒããªãã®é¢ä¿ã«ããã
æ瀺çã«äŒããïŒã¢ãããŒã·ã§ã³ïŒ
let a: number = 1 // aã¯number
let b: string = 'hello' // bã¯string
æå°éã«ããŠãæ§ããªã
let a = 1 // aã¯number
let b = 'hello' // bã¯string
TypeScriptã¯åãæšè«ããã®ãåŸæãªã®ã§ãã¢ãããŒã·ã§ã³ã¯å¿ èŠãªå Žåã®ã¿ã§ã倧äžå€«
3ç« åã«ã€ããŠ
âTypeScriptã®åã®éå±€æ§é
åŒçš : ããã°ã©ãã³ã°TypeScript.. OREILLY
any
TypeScriptã§ã¯ãã³ã³ãã€ã«æã«ãã¹ãŠã®ãã®ãåãæã£ãŠããå¿
èŠãããã
ãããã®ã®åãããããªãå Žåã®ããã©ã«ãã®åãany
ã«ãªãã
unknown
åãã£ãŠæ¬åœã«åãããããªãå Žåã¯ãunknown
ã䜿çšããã
any
ãšåæ§ã«ä»»æã®å€ãè¡šããããããäœã§ããããã§ãã¯ããããšã§çµã蟌ããŸã§TypeScriptã§ã¯ãunknown
åã®å€ã®äœ¿çšãèš±å¯ããªããâ»any
å³éç
let a: unknown = 10 // unknown
let b = a + 5 // ãšã©ãŒ
// ããã§typeofã䜿çšããããšã§äžèšã®10ã®ä»£å
¥ãå
ã«numberåã«ç¢ºå®ãã
if (typeof a === 'number') {
let c = a + 5 // number
}
boolean
true
ãfalse
ã®2ã€ã®å€ããããããããæ¯èŒãåŠå®ãªã©ã¯ã§ãããã»ãã¯ããŸãã§ããªãã
let a = true // boolean
const b = true // true
let c: boolean = true // boolean
let d: true = true // true
ããã§æ³šç®ãã¹ãã¯ãæåŸã®è¡ã®ã³ãŒã let d: true = true
ã®éšåã§ããã
ããã¯ãããã ã®boolean
ã§ã¯ãªããç¹å®ã®boolean true
ã ãããšãã詳ããæå®ããŠããã
â»å³å¯ã«åã決ããŠããããã®ãããªããšããªãã©ã«åãšããã
ãã1ã€ã®ãã€ã³ãã¯ãconst
ã䜿çšããŠãããšãããªãã©ã«åã«ãªã£ãŠããããš
const
ã䜿çšãããšå€æŽããªãããšãTypeScriptãç¥ã£ãŠããã®ã§ãæãçãåãæšè«ããã
ãªãã©ã«åãšã¯ããã 1ã€ã®å€ãè¡šãããã以å€ã®å€ã¯åãå ¥ããªãå
number
æŽæ°ãæµ®åå°æ°ç¹æ°ãæ£æ°ãè² æ°ãInfinityãNaNïŒéæ°ïŒãªã©ãã¹ãŠã®æ°å€ã®éãŸã
+, -, %, < ãªã©ãå«ãŸããã
let a = 1234 // number
const b = 5678 // 5678
let c = a > b //boolean
bigint
äžžãã®ãšã©ãŒã«ééããããšãªãã倧ããªæŽæ°ãæ±ããã
number
ã§ã¯ã2ã®53ä¹ãã§ããè¡šããªãããbigint
ãªããã£ãšå€ãã®æŽæ°ãæ±ããã
``javascript
let a = 1234n // bigint
let b = 22.4n // ãšã©ãŒ bigintã¯æŽæ°ããæ±ããªã
let c: 100n = 100n // 100n
:::message alert
å¯èœãªéããbigintåã¯TypeScriptã«æšè«ãããããã«ãã
:::
### string
ãã¹ãŠã®æååãšãããã䜿ã£ãŠã§ããããšïŒé£çµãã¹ã©ã€ã¹ïŒã®éãŸã
```javascript
let a = 'hello' // string
const b = '!' // '!'
let c = a + ' ' + b // string
symbol
䜿çšé »åºŠã¯å°ãªãã
ãªããžã§ã¯ãããããã«ãããŠæååã®ããŒã®ä»£ãããšããŠãæ¢åã®ããŒãé©åã«äœ¿ããããã£ãã誀ã£ãå€ãèšå®ãããŠããªãããšã匷ã確信ããå Žåã«äœ¿çšãããã
let sym2 = Symbol("key");
let sym3 = Symbol("key");
sym2 === sym3; // false, symbols are unique
let sym = Symbol();
let obj = {
[sym]: "value"
};
console.log(obj[sym]); // "value"
ãªããžã§ã¯ã
ãªããžã§ã¯ãã®åœ¢ç¶ãæå®ããã
ãã詳ãã...
// 以äžã ãšãšã©ãŒã«ãªã
let a: object = {
b: 'x'
}
a.b //ããšã©ãŒ
ããããªããžã§ã¯ãã£ãŠãªãã®åœ¹ã«ç«ã€ã®ã
object
ã¯ãany
ã®å°ãéå®ãããŠããã®ã§ããã»ã©å€ãããªãã
以äžã®ããã«ãªããžã§ã¯ããåä»ãã§ããïŒãªããžã§ã¯ããªãã©ã«åïŒ
let a: {b: number} = {
b: 12
} // {b: number}
...å€ãã®ããããã£ããå¯èœæ§ãããå Žåã¯ã©ãããã
let a: {
b: number
// aã¯stringã§ããããããã£cãæã€å¯èœæ§ãããã
c?: string
[key: number]: boolean
}
ã€ã³ããã¯ã¹ã·ã°ããã£ãšã¯ã
[key: T]: U
ã¿ãããªæ§æã®ããš
ãªããžã§ã¯ããå€ãã®ããŒãå«ãå¯èœæ§ãããããšãTypeScriptã«äŒããæ¹æ³
â»key
ã¯ä»»æã®ååã§ã䜿çšã§ããã
TypeScriptã®ãªããžã§ã¯ã宣èšãŸãšã
- ãªããžã§ã¯ããªãã©ã«è¡šèšïŒ
{a: string}
ã¿ãããªïŒ- ãªããžã§ã¯ããã©ããªãã£ãŒã«ããæã€ããããæ
- ãªããžã§ã¯ãã®å€ããã¹ãŠåãåãæã€æ
- objectåãªããžã§ã¯ããã©ããªãã£ãŒã«ããæã€ãéèŠããªãå Žåã«äœ¿çšãã
- 空ã®ãªããžã§ã¯ããªãã©ã«ã¯äœ¿çšããªãïŒ
{}
ïŒ - Objectåã¯äœ¿çšããªããäžèšãšåã
åãšã€ãªã¢ã¹
å€æ°å®£èšïŒlet, constãšãïŒã䜿çšããŠãå€ã®ãšã€ãªã¢ã¹ãšãªã宣èšãã§ãã
䜿çšããå©ç¹
- ååãçŽæçã«ãªã
- è€éãªæ¹ãäžåºŠã®å®£èšã§ããã®ã§DRYã«æžãã
-
let
ãconst
ãšåæ§ã«ãããã¯ã¹ã³ãŒããªã®ã§æ±ãããã
type Age = number
type Person = {
name: string
age: Age
}
// Personãšãããªããžã§ã¯ãã®å®çŸ©ãç解ãããããªã
// 以äžã®ããã«ãã§ãã
let age = 25
let driver: Person = {
name: 'James Role',
age: age
}
å䜵åïŒå ±çšäœåãå ±çšåãunionåïŒãšäº€å·®å
å䜵ã¯ã|
亀差ã¯ã&
type Cat = { name: string, purrs: boolean }
type Dog = { name:string, barks: boolean, wags: boolean }
type CatOrDogOrBoth = Cat | Dog
// Cat
let a: CatOrDogOrBoth = {
name: 'Bonkers',
purrs: true
}
// Dog
a = {
name: 'Domino',
barks: true,
wags: true
}
// äž¡æ¹
a = {
name: 'Donkers',
barks: true,
purrs: true,
wags: true
}
é å
èŠçŽ ã®é£çµãè¿œå ãæ€çŽ¢ãã¹ã©ã€ã¹ã®ãããªæäœããµããŒããã
let a = [1, 2, 3] // number[]
var b = ['a', 'b'] // string[]
const c = [2, 'b'] // (string | number)[]
let d = []
d.push(1)
d.push(true) // ãšã©ãŒ
let e: number[] = [] // number[]
e.push(1) // number[]
e.push('red') // ãšã©ãŒ
TypeScriptã§é åã䜿çšããå Žåã¯ã
T[]
ãšArray<T>
ãããã
ããã¯ãããã©ãŒãã³ã¹çããŸã£ããåã
é å > ã¿ãã«
å¯å€é·ã®é åãåä»ãããããã®æ¹æ³
let trainFares: [number, number?][] = [
[2.35],
[3.45, 5.40],
[6.75]
]
// å°ãªããŠã1ã€ã®èŠçŽ ãæã€ãæååã®ãªã¹ã
let friends: [string, ...string[]] = ['Iru', 'Tura', 'Mina', 'Kima' ]
// äžåäžãªãªã¹ã
let list: [number, boolean, ...string[]] = [1, false, 'w', 'e']
null, undefined, void, never
ããããã®éã
- undefinedïŒãããã®ããŸã å®çŸ©ãããŠããªãïŒ
- nullïŒå€ãæ¬ åŠããŠããïŒ
- voidïŒæ瀺çã«äœãè¿ããªãé¢æ°ã®æ»ãå€ã®åïŒ
- neverïŒæ±ºããŠæ»ãããšã®ãªãé¢æ°ã®åïŒ
enumïŒæ®æåïŒ
ããå€ã«ã€ããŠåãåŸãå€ãåæããæ¹æ³
æ £äŸã§ãenumã¯å€§æåã§å§ããåæ°åœ¢
enum Language {
English,
Japanese,
Spanish
}
// æ瀺çã«å€ãèšå®ã§ãã
enum Language {
English = 0,
Japanese = 1,
Spanish = 2
}
4ç« é¢æ°
é¢æ°ã®å®£èšã»åŒã³åºã
JSã§ã¯ãé¢æ°ã¯ç¬¬äžçŽã®ãªããžã§ã¯ãã ããä»ã®ãªããžã§ã¯ããšå
šãåãããã«æ±ããã
éåžžã¯ãé¢æ°ã®ãã©ã¡ãŒã¿ãã¢ãããŒããã
// ååä»ãé¢æ°
function greet(name: string) {
return 'hello' + name
}
// ã¢ããŒé¢æ°åŒ
let greet2 = (name: string) => {
'hello' + name
}
// (a: number, b: number)ã¯åŒæ°ã§ããã¯å¿
ãèšè¿°ãã
// (...): number ããã¯æ»ãå€ã®åãä»»æã§ãã
let add = (a: number, b: number): number {
a + b
}
ãªãã·ã§ã³ãã©ã¡ãŒã¿
ã?ãã䜿çšããŠããã©ã¡ãŒã¿ãçç¥å¯èœã«æå®ããããšãã§ããã
function distance(p1: Point, p2?: Point): number {
return ((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2) ** (1 / 2);
}
distance(q1, q2);
distance(q1);
ããã©ã«ããã©ã¡ãŒã¿
ã?ãã=...ã®ãããªèšè¿°ã«å€æŽãããšããã©ã«ãå€ãèšå®ã§ããã
function log(message: string, userId = 'Not signed in') {
console.log(message, userId)
}
log('User signed out')
ã¬ã¹ããã©ã¡ãŒã¿
ããé¢æ°ãåŒæ°ã®ãªã¹ããåãå Žåããã®ãªã¹ããé åãšããŠæž¡ããã
function sum(numbers: number[]): number {
return numbers.reduce((total, n) => total + n, 0)
}
sum([1, 2, 3]) // 6
// å¯å€é·åŒæ°ãåãåãããæã«ãã¬ã¹ããã©ã¡ãŒã¿ã䜿çšããã
function sum(...numbers: number[]): number {
return numbers.reduce((total, n) => total + n, 0)
}
sum([1, 2, 3]) // 6
call, apply, bind
äžžæ¬åŒ§()ãã€ããŠåŒã¶ä»ã«ãæ¹æ³ãããããããäžèšã®ã¡ãœããã詳ããã¯çç¥
thisã®åä»ã
TypeScriptã§ã¯
é¢æ°ã§this
ã䜿çšããå Žåã¯ãæåŸ
ããthis
ã®åããé¢æ°ã®æåã®ãã©ã¡ãŒã¿ãŒãšããŠå®£èšããã
ãžã§ãã¬ãŒã¿ãŒ
äžé£ã®å€ãçæããããã®æ¹æ³
ãžã§ãã¬ãŒã¿ãŒã¯ããžã§ãã¬ãŒã¿ãŒé¢æ°ã«ãã£ãŠçæãããã
function* createFibonacciGenerator() {
let a = 0
let b = 1
white (true){
yield a;
[a, b] = [b, a + b]
}
}
let fibonacciGenerator = createFibonaciGenerator() // Generator<number>
fibonacciGenerator.next() // { value: 0, done: false }
.....
ã€ãã¬ãŒã¿ãŒ
ããå€ãå©çšããããã®æ¹æ³ïŒãžã§ãã¬ãŒã¿ãŒã¯çæã«å¯ŸããŠïŒ
å埩å¯èœãªããžã§ã¯ãïŒIterableïŒãšã¯ã
Symbol.iterator
ãšããããããã£ãå«ãã§ãããªããžã§ã¯ãããã®ããããã£ã®å€ã¯ãã€ãã¬ãŒã¿ãŒãè¿ãé¢æ°
ã€ãã¬ãŒã¿ãŒãšã¯ãnextãšåŒã°ããã¡ãœãããå®çŸ©ããŠãããªããžã§ã¯ããnextã¡ãœããã¯ãvalue, doneãšããããããã£ãæã€ãªããžã§ã¯ããè¿ã
åŒã³åºãã·ã°ããã£
é¢æ°ãã®ãã®ã®å®å
šãªåãè¡šçŸããæ¹æ³
ãŸã以äžãèŠãŠã¿ãã
function add(a: number, b: number): number {
return a + b
}
ãã®add
ã®åã¯ãªã«ã... Function
ã§ãã
Function
ã¯ãã¹ãŠã®é¢æ°ã®å
æ¬çãªåãªã®ã§ãç¹å®ã®é¢æ°ã«ã€ããŠãªã«ãããããªãã
ãããªæãTypeScriptã§ã¯ã以äžã®ããã«è¡šçŸããã
(a: number, b:number) => number
åã¬ãã«ãå€ã¬ãã«ã«ã€ããŠã¯æžç±P59ãåç §
æèçåä»ã
é¢æ°ã®ãã©ã¡ãŒã¿ãŒã®åãæ瀺çã«ã¢ãããŒãããªããã¿ãŒã³
- åŒã³åºãã·ã°ããã£ã䜿çšããŠ
log: Log
ã®ããã«å®£èšããŠããå ŽåïŒäºåã«Log
ã宣èšããŠãããã®ãšããïŒ - ã³ãŒã«ããã¯é¢æ°ã®å ŽåïŒããé¢æ°ã«å€ãæž¡ãæãå®è¡é¢æ°ãã€ã³ã©ã€ã³ã§å®£èšããŠããå Žåããã®é¢æ°ãæ瀺çã«ã¢ãããŒãããå¿ èŠããªãïŒ
ãªãŒããŒããŒããããé¢æ°ã®å
è€æ°ã®åŒã³åºãã·ã°ããã£ãæã€é¢æ°ã®ããš
ããã¯æ·±å ãå¿
èŠ...
4ç« - 2
ããªã¢ãŒãã£ãºã
ããªã¢ãŒãã£ãºã ãšã¯ãããã°ã©ãã³ã°èšèªã®æã€æ§è³ªã®äžã€ã§ãããé¢æ°ãã¡ãœãããªã©ããåŒæ°ãè¿ãå€ã®æ°ãããŒã¿åãªã©ã®ç°ãªãè€æ°ã®å®è£ ãæã¡ãåŒã³åºãæã«äœ¿ãåããããã«ã§ããããš
é¢æ°ã«ã€ããŠãå ·äœçãªåãããããªããšãã«ã©ãããã
ãžã§ããªãã¯åãšã¯
- ãžã§ããªãã¯åãã©ã¡ãŒã¿ã¯
(<>)
ã§å²ã£ãŠäœ¿çšãã - å±±æ¬åŒ§ã®å Žæã«ãã£ãŠã¹ã³ãŒãã決ãŸã
- ã¹ã³ãŒãå ã§ãžã§ããªãã¯åãã©ã¡ãŒã¿ã®ãã¹ãŠã®ã€ã³ã¹ã¿ã³ã¹ãæçµçã«åãå ·äœçãªåã«ãã€ã³ããã
åé§åéçº
ãããããšã³ãŒããªãŒãã£ã³ã°ã§ããã¬ãã«ã«ãªããªããšãŸãã
æ¡çšé »åºŠãããªãå€ãããã«æãã
éã«ãããããããã§ããã°é¢æ°å
ã®ã³ãŒããèŠãªããŠãåã§å
±åãçŽæçã«å€æã§ãã