TypeScript 2.4 RCããªãªã¼ã¹ããã¾ããã
Announcing TypeScript 2.4 RC | TypeScript
ããã¤ãå¤æ´ç¹ãããã®ã§ãããã®ã¨ã³ããªã§ã¯Safer callback parameter checkingã«ã¤ãã¦è§£èª¬ãã¾ããå ¬å¼ããã¥ã¡ã³ãã§ããã¨FAQ · Microsoft/TypeScript Wiki · GitHubãããã®è©±ã«é¢é£ãã¾ãããããã¯なぜ TypeScript の型システムが健全性を諦めているかã¨ãé¢é£ãã¾ãã
Dog[]
㯠Animal[]
ã®ãµãã¿ã¤ãã
TSã§ã¯ Dog
ã Animal
ã®ãµãã¿ã¤ãã§ããæãDog[]
㯠Animal[]
ã®ãµãã¿ã¤ãã§ããåã·ã¹ãã ã¨ãã¦å¥å
¨ãã©ããã¯ç½®ãã¨ãã¦ä¾¿å©ãªã®ã§ãããªã£ã¦ãã¾ãã
ãã¦ãDog[]
ã Animal[]
ã«ä»£å
¥å¯è½ãã©ãããå¤å®ããéã³ã³ãã¤ã©ã¯æçµçã«ã(x: Dog) => number
㯠(x: Animal) => number
ã«ä»£å
¥å¯è½ãã調ã¹ããã¨ã«ãªãã¾ããã§ã¯ãããã©ããã£ã¦å¤å®ããã®ã§ããããï¼çãã¯ãDog
ãAnimal
ã«ä»£å
¥å¯è½ãããã¯Animal
ãDog
ã«ä»£å
¥å¯è½ããã§å¤å®ãã¾ãã
ã¤ã¾ããDog
ãAnimal
ã«ä»£å
¥å¯è½ãããã¯Animal
ãDog
ã«ä»£å
¥å¯è½ ãªãã°(x: Dog) => number
ã¯(x: Animal) => number
ã«ä»£å
¥å¯è½ãã¨ãããã¨ã§ãã
é話
æ¬çã¨ã¯ãºã¬ã¾ãããDog[]
㯠Animal[]
ã®ãµãã¿ã¤ãã¨ããã¨ãã«ãåã·ã¹ãã ã¨ãã¦å¥å
¨æ§ãå´©ããä¾ã§ãã
class Animal { } class Dog extends Animal { bark(): void {} } let a = [new Animal] let d = [new Dog] a = d; a[0] = new Animal for ( let item of d) { item.bark() } // => Uncaught TypeError: item.bark is not a function
ãã®ä»æ§ã®åé¡ç¹
Dog[]
㯠Animal[]
ã®ãµãã¿ã¤ãã§ãããã¨ã®åé¡ç¹ã¯ãã¦ããããDog
ãAnimal
ã«ä»£å
¥å¯è½ãããã¯Animal
ãDog
ã«ä»£å
¥å¯è½ ãªãã°(x: Dog) => number
ã¯(x: Animal) => number
ã«ä»£å
¥å¯è½ãã¨ãªããã¨ãåé¡ã«ãªãã¾ãã
ã¢ãã¦ã³ã¹ããã°ããã®å¼ç¨ã§ããã¨
interface Animal { animalStuff: any } interface Dog extends Animal { bark(): void } interface BasicCollection<T> { forEach(callback: (value: T) => void): void; } declare let animalCollection: BasicCollection<Animal>; declare let dogCollection: BasicCollection<Dog>; // This should be an error, but TypeScript 2.3 and below allow it. dogCollection = animalCollection;
dogCollection
ã«animalCollection
ã代å
¥å¯è½ãã©ããã¯æçµçã«ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹BasicCollection
ã®forEach
ã®å¼æ°ã«ãªã£ã¦ããcallback
é¨åã代å
¥å¯è½åã©ããã§å¤å®ããã¾ããã¤ã¾ãcallback: (value: Dog) => void
ã«callback: (value: Animal) => void
ã代å
¥å¯è½ãã©ããã§ãããé
åã®ä¾ã§è¦ãæã¨åããã¸ãã¯ã§ä»£å
¥å¯è½ã¨å¤æããdogCollection = animalCollection
ã¯ã¨ã©ã¼ã«ãªãã¾ããã
å
·ä½çã«callback
ã
dogCollection.forEach((value: Dog) => {value.bark()});
ã ã¨æ³å®ããã¨å®è¡æã«ã¨ã©ã¼ã«ãªãã®ããããã¨æãã¾ãã
ããã¯Promise<T>
ã«ãããthen
ã«ãå½ã¦ã¯ã¾ãã¾ãã®ã§ãPromise<Animal>
ãPromise<Dog>
ã«ä»£å
¥å¯è½ãªãã¨ã«ãªã£ã¦ãã¾ãã¾ãã
2.4ããã©ããªãã®ã
callbacké¢æ°ã®å¤å®æã¯ç¹å¥ã«(x: Dog) => void
㯠(x: Animal) => void
ã«ä»£å
¥å¯è½ãã®å¤å®ãAnimal
ãDog
ã«ä»£å
¥å¯è½ãã§å¤å®ãããããã«ãªãã¾ãã
ã¨ããããã§dogCollection = animalCollection
ã¯ã¨ã©ã¼ã¨ãªããanimalCollection = dogCollection
ã¯(å¼ãç¶ã)ã¨ã©ã¼ã«ãªãã¾ãããã¾ãPromise<Animal>
ãPromise<Dog>
ã«ä»£å
¥ãããã¨ãã§ããªããªãã¾ãã
TS 2.4以å(playdroundãã¢ãããã¼ããããã¾ã§ã¯ã¨ã©ã¼ãåºãªãæ§åãè¦ããã¨æãã¾ã)ã
2.4以éã¯ä»¥ä¸ã
interface Animal { } interface Dog extends Animal { someProperty: string } let a: Promise<Animal>; let d: Promise<Dog>; d = a; // => // a.ts(11,1): error TS2322: Type 'Promise<Animal>' is not assignable to type 'Promise<Dog>'. // Type 'Animal' is not assignable to type 'Dog'. // Property 'someProperty' is missing in type 'Animal'.
ã¾ã¨ã
Dog[]
ãAnimal[]
ã®ãµãã¿ã¤ãã«ããããã«Promise<Animal>
ãPromise<Dog>
ã«ä»£å
¥å¯è½ã§ãããã2.4ããã¯Promise<Animal>
ã¯Promise<Dog>
ã«ä»£å
¥ã§ããªããªãã¾ããDog[]
ãAnimal[]
ã®ãµãã¿ã¤ãã§ãããã¨ã¯ãã®ã¾ã¾ã§ãã