@@ -99,7 +99,7 @@ mocha+power-assertでテストを書く場合を例に、使い方を解説し
9999テスト対象のコードは@< code> {usage/ lib/ index}です(@< list> {usage/ lib/ index})。
100100
101101// list[usage/lib/index][至って普通の外部モジュール]{
102- #@# TODO #@ mapfile(../ code- 2 . 0 / definition- file/ usage/ lib/ index. ts)
102+ #@# TODO #@ mapfile(../ code/ definition- file/ usage/ lib/ index. ts)
103103"use strict" ;
104104
105105export function hello(word = "TypeScript" ) {
@@ -112,7 +112,7 @@ export function hello(word = "TypeScript") {
112112普通ですね。「特定のinputを与えるとoutputが得られる」ことを検証するコードです。
113113
114114// list[usage/tests/indexSpec][mocha+power-assertでテストを書く]{
115- #@# TODO #@ mapfile(../ code- 2 . 0 / definition- file/ usage/ tests/ indexSpec. ts)
115+ #@# TODO #@ mapfile(../ code/ definition- file/ usage/ tests/ indexSpec. ts)
116116// / <reference path="../typings/mocha/mocha.d.ts" />
117117// / <reference path="../typings/power-assert/power-assert.d.ts" />
118118
@@ -147,7 +147,7 @@ JavaScriptの世界では静的な型検査などありませんので問題あ
147147mocha(@< list> {usage/ abstract/ mocha})とpower- assert (@< list> {usage/ abstract/ power- assert })の型定義ファイル(抜粋)を見てみましょう。
148148
149149// list[usage/abstract/mocha][mocha.d.ts抜粋]{
150- #@# TODO #@ mapfile(../ code- 2 . 0 / definition- file/ usage/ abstract/ mocha. d. ts)
150+ #@# TODO #@ mapfile(../ code/ definition- file/ usage/ abstract/ mocha. d. ts)
151151interface MochaDone {
152152 (error?: Error): void;
153153}
@@ -167,7 +167,7 @@ declare var it: {
167167// }
168168
169169// list[usage/abstract/power-assert][power-assert.d.ts抜粋]{
170- #@# TODO #@ mapfile(../ code- 2 . 0 / definition- file/ usage/ abstract/ power- assert .d. ts)
170+ #@# TODO #@ mapfile(../ code/ definition- file/ usage/ abstract/ power- assert .d. ts)
171171declare function assert (value: any , message?: string): void;
172172
173173declare module "power-assert" {
@@ -276,7 +276,7 @@ TypeScriptを書き始めの頃は、品質を気にした所で後々粗が見
276276というのも、インタフェースには@< strong> {後から定義を拡張できる}という特性があるからです(@< list> {interface/ declarationMerging}、@< list> {interface/ declarationMergingUsage})。
277277
278278// list[interface/declarationMerging][定義を分割して書く]{
279- #@ mapfile(../ code- 2 . 0 / definition- file/ interface/ declarationMerging. d. ts)
279+ #@ mapfile(../ code/ definition- file/ interface/ declarationMerging. d. ts)
280280interface Foo {
281281 hello() : string;
282282}
@@ -289,7 +289,7 @@ interface Foo {
289289// }
290290
291291// list[interface/declarationMergingUsage][定義が統合される!]{
292- #@ mapfile(../ code- 2 . 0 / definition- file/ interface/ declarationMergingUsage. ts)
292+ #@ mapfile(../ code/ definition- file/ interface/ declarationMergingUsage. ts)
293293// / <reference path="./declarationMerging.d.ts" />
294294// ↑ 昔はこのようにreferece commentを使ってファイル間の依存関係を明示していましたが、
295295// 最近はtsconfig.jsonに全ての依存関係を書くようにしたため見かける事が大変少なくなりました
@@ -312,7 +312,7 @@ String#trimStartは、文字列の先頭にある空白文字を取り除く機
312312そのため、String インタフェースを拡張する形でコンパイルを通せるようにしてみましょう(@< list> {interface/ stringTrimStart})
313313
314314// list[interface/stringTrimStart][String#trimStartを生やす]{
315- #@ mapfile(../ code- 2 . 0 / definition- file/ interface/ stringTrimStart. ts)
315+ #@ mapfile(../ code/ definition- file/ interface/ stringTrimStart. ts)
316316interface String {
317317 trimStart() : string;
318318}
@@ -340,7 +340,7 @@ namespaceを作ったとしても、即座に実体が生成されるとは限
340340namespaceが抱えるのがインタフェースのみである場合、実体がある扱いにはならないのです(@< list> {ghost- module/ invalid})。
341341
342342// list[ghost-module/invalid][幽霊モジュール]{
343- #@ mapfile(../ code- 2 . 0 / definition- file/ ghostModule/ invalid. ts)
343+ #@ mapfile(../ code/ definition- file/ ghostModule/ invalid. ts)
344344declare namespace ghost {
345345 interface Test {
346346 str: string;
@@ -365,7 +365,7 @@ export { }
365365@< list> {ghostModule/ jqueryWithoutGhostModule- ignore }はjQueryの型定義ファイルからの抜粋(&一部改変)です。
366366
367367// list[ghostModule/jqueryWithoutGhostModule-ignore][実際のjQueryの型定義の例]{
368- #@ mapfile(../ code- 2 . 0 / definition- file/ ghostModule/ jqueryWithoutGhostModule- ignore .d. ts)
368+ #@ mapfile(../ code/ definition- file/ ghostModule/ jqueryWithoutGhostModule- ignore .d. ts)
369369interface JQuery {
370370 addClass(className: string ): JQuery ;
371371 html(htmlString: string ): JQuery ;
@@ -413,7 +413,7 @@ IDE上で型注釈を手書きするときも候補がたくさんサジェス
413413これを幽霊モジュールを使って書きなおしてみます(@< list> {ghostModule/ jqueryWithGhostModule- ignore })。
414414
415415// list[ghostModule/jqueryWithGhostModule-ignore][幽霊モジュールを使ってみた]{
416- #@ mapfile(../ code- 2 . 0 / definition- file/ ghostModule/ jqueryWithGhostModule- ignore .d. ts)
416+ #@ mapfile(../ code/ definition- file/ ghostModule/ jqueryWithGhostModule- ignore .d. ts)
417417declare namespace jquery {
418418 interface Element {
419419 addClass(className: string ): Element ;
@@ -486,7 +486,7 @@ declare var $: jquery.Static;
486486具体的に、モジュール様の構造をインタフェースを使って作ってはいけません(@< list> {interfaceAntipattern/ moduleByInterfaceBad- ignore })。
487487
488488// list[interfaceAntipattern/moduleByInterfaceBad-ignore][インタフェースでモジュールを表現してしまう。何故なのか…]{
489- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ moduleByInterfaceBad- ignore .d. ts)
489+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ moduleByInterfaceBad- ignore .d. ts)
490490interface Foo {
491491 bar: FooBar ;
492492}
@@ -512,7 +512,7 @@ declare var foo: Foo;
512512普通に、@< list> {interfaceAntipattern/ moduleByInterfaceGood- ignore }のように書きましょう。
513513
514514// list[interfaceAntipattern/moduleByInterfaceGood-ignore][素直にこうしよう]{
515- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ moduleByInterfaceGood- ignore .d. ts)
515+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ moduleByInterfaceGood- ignore .d. ts)
516516// 普通にコレでいいだろ!!
517517declare namespace foo.bar.buzz {
518518 var str: string;
@@ -527,7 +527,7 @@ declare namespace foo.bar.buzz {
527527#@# TODO 内部モジュールで全文を検索 module とか declare module でも
528528
529529// list[interfaceAntipattern/callableModuleUsage-ignore][関数・namespace どっちなの?]{
530- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ callableModuleUsage- ignore .ts)
530+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ callableModuleUsage- ignore .ts)
531531// assertは関数としても呼べるしnamespaceのようにも見える
532532assert(foo === "foo" );
533533assert.ok(value);
@@ -538,7 +538,7 @@ assert.ok(value);
538538この場合、すぐに考えつく型定義は@< list> {interfaceAntipattern/ callableModuleBad1- ignore }か、@< list> {interfaceAntipattern/ callableModuleBad2- ignore }でしょう。
539539
540540// list[interfaceAntipattern/callableModuleBad1-ignore][こうしてしまいたい、気持ち]{
541- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ callableModuleBad1- ignore .d. ts)
541+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ callableModuleBad1- ignore .d. ts)
542542declare var assert: {
543543 (value: any ): void;
544544 ok(value: any ): void;
@@ -547,7 +547,7 @@ declare var assert: {
547547// }
548548
549549// list[interfaceAntipattern/callableModuleBad2-ignore][匿名型注釈よりはマシ]{
550- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ callableModuleBad2- ignore .d. ts)
550+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ callableModuleBad2- ignore .d. ts)
551551declare var assert: Assert ;
552552
553553interface Assert {
@@ -563,7 +563,7 @@ interface Assert {
563563しかし、これには別のよいやり方があるのです(@< list> {interfaceAntipattern/ callableModuleGood- ignore })。
564564
565565// list[interfaceAntipattern/callableModuleGood-ignore][関数とnamespace 両方やらなきゃいけないのが辛いところだ]{
566- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ callableModuleGood- ignore .d. ts)
566+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ callableModuleGood- ignore .d. ts)
567567declare function assert(value: any ): void;
568568declare namespace assert {
569569 function ok(value: any ): void;
@@ -580,7 +580,7 @@ declare namespace assert {
580580@< list> {interfaceAntipattern/ powerAssertAbst- ignore }に抜粋& 改変したものを示します。
581581
582582// list[interfaceAntipattern/powerAssertAbst-ignore][関数+内部モジュールの実例]{
583- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ powerAssertAbst- ignore .d. ts)
583+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ powerAssertAbst- ignore .d. ts)
584584declare function assert (value: any , message?: string): void;
585585declare module assert {
586586
@@ -605,7 +605,7 @@ declare module assert {
605605実は、このやり方は型定義ファイルだけではなく、通常のTypeScript コードでも使えます(@< list> {interfaceAntipattern/ callableModule. ts})。
606606
607607// list[interfaceAntipattern/callableModule.ts][関数が先、namespaceは後!絶対!]{
608- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ callableModule. ts)
608+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ callableModule. ts)
609609function test() {
610610 return "test!" ;
611611}
@@ -620,7 +620,7 @@ namespace test {
620620コンパイル結果の@< list> {interfaceAntipattern/ callableModule. js}を見ると、なぜ関数が先でnamespaceが後、という決まりになっているかがわかりますね。
621621
622622// list[interfaceAntipattern/callableModule.js][JSとして正しい構造だ]{
623- #@ mapfile(../ code- 2 . 0 / definition- file/ interfaceAntipattern/ callableModule. js)
623+ #@ mapfile(../ code/ definition- file/ interfaceAntipattern/ callableModule. js)
624624function test() {
625625 return "test!" ;
626626}
@@ -643,7 +643,7 @@ var test;
643643まずはその2 つのやり方を見てみましょう(@< list> {declareClass/ basic})。
644644
645645// list[declareClass/basic][素直にクラス定義 vs インタフェース+変数]{
646- #@ mapfile(../ code- 2 . 0 / definition- file/ declareClass/ basic. d. ts)
646+ #@ mapfile(../ code/ definition- file/ declareClass/ basic. d. ts)
647647// A. 普通にクラスを定義する
648648declare class TestA {
649649}
@@ -666,7 +666,7 @@ interface TestB {
666666よい時代になったものです。
667667
668668// list[declareClass/stretch][相互運用性がある!]{
669- #@ mapfile(../ code- 2 . 0 / definition- file/ declareClass/ stretch. ts)
669+ #@ mapfile(../ code/ definition- file/ declareClass/ stretch. ts)
670670// classはopen-endedになったため同名のinterfaceで拡張可能に
671671class Person {
672672 name: string;
@@ -723,7 +723,7 @@ export { }
723723どれが一番、元々の関数の仕様がわかりやすいですか?
724724
725725// list[overload/useOverload][普通に使えます]{
726- #@ mapfile(../ code- 2 . 0 / definition- file/ overload/ useOverload. ts)
726+ #@ mapfile(../ code/ definition- file/ overload/ useOverload. ts)
727727// 同じ実装に対して、どの型定義が一番便利かな?
728728// 1関数でget, set両方の役目を果たす場合…
729729
@@ -753,7 +753,7 @@ union typesを使うと、@<list>{overload/overloadVsUnionTypes}のように書
753753簡単な例だとunion typesのほうがよいと思いますが、このケースではどっちがいいかは、今の知見ではまだわからないですね。
754754
755755// list[overload/overloadVsUnionTypes][うーん、どっちがいいかは難しい]{
756- #@ mapfile(../ code- 2 . 0 / definition- file/ overload/ overloadVsUnionTypes. ts)
756+ #@ mapfile(../ code/ definition- file/ overload/ overloadVsUnionTypes. ts)
757757// union types未使用
758758declare function hello(word: string ): string;
759759declare function hello(callback: () => string ): string;
@@ -780,7 +780,7 @@ bye(() => "function");
780780めでたい。
781781
782782// list[externalModuleDeclarationMerging/basic][モジュール定義を後から拡張可能]{
783- #@ mapfile(../ code- 2 . 0 / definition- file/ externalModuleDeclarationMerging/ basic. d. ts)
783+ #@ mapfile(../ code/ definition- file/ externalModuleDeclarationMerging/ basic. d. ts)
784784// モジュールの定義の統合ができます
785785declare module "foo" {
786786 let str : string ;
@@ -793,7 +793,7 @@ declare module "foo" {
793793// }
794794
795795// list[externalModuleDeclarationMerging/usage][普通に使えます]{
796- #@ mapfile(../ code- 2 . 0 / definition- file/ externalModuleDeclarationMerging/ usage. ts)
796+ #@ mapfile(../ code/ definition- file/ externalModuleDeclarationMerging/ usage. ts)
797797import * as foo from "foo" ;
798798foo. str;
799799foo. num;
@@ -848,7 +848,7 @@ Visual StudioなどのIDEでは、型定義ファイル上に書かれたJSDoc
848848まずは例を見てみましょう(@< list> {callback/ basic})。
849849
850850// list[callback/basic][optionalはもしかしたら値がないことを表す]{
851- #@ mapfile(../ code- 2 . 0 / definition- file/ callback/ basic. ts)
851+ #@ mapfile(../ code/ definition- file/ callback/ basic. ts)
852852// 良い例
853853declare function readFile(filePath: string , listener: (data : string ) => void ): void;
854854// 悪い例
@@ -906,7 +906,7 @@ C#やJavaよりも、広い範囲でインタフェースが利用されるの
906906インタフェースやクラスのインスタンス単体をモジュールの外側に見せたい場合、@< list> {export/ sample1}のように書きます。
907907
908908// list[export/sample1][実はインタフェースBarも外から見えない]{
909- #@ mapfile(../ code- 2 . 0 / definition- file/ export/ sample1. d. ts)
909+ #@ mapfile(../ code/ definition- file/ export/ sample1. d. ts)
910910declare module "bar" {
911911 interface Bar {
912912 num: number;
@@ -923,7 +923,7 @@ declare module "bar" {
923923importした値がインタフェースFoo のインスタンスになっていることがわかります。
924924
925925// list[export/sample1Usage][使うとき。インタフェースBarのインスタンスが得られる]{
926- #@ mapfile(../ code- 2 . 0 / definition- file/ export/ sample1Usage. ts)
926+ #@ mapfile(../ code/ definition- file/ export/ sample1Usage. ts)
927927// b は "bar" の Barのインスタンス だよ!
928928import * as b from "bar" ;
929929b. num;
@@ -934,7 +934,7 @@ b.num;
934934インタフェースのインスタンスをexportしたつもりが型がexportされてしまうのです。
935935
936936// list[export/sample2][それは値ではなくて型だけexportしているぞ!]{
937- #@ mapfile(../ code- 2 . 0 / definition- file/ export/ sample2. d. ts)
937+ #@ mapfile(../ code/ definition- file/ export/ sample2. d. ts)
938938declare module "buzz" {
939939 interface Buzz {
940940 num: number;
@@ -964,7 +964,7 @@ TypeScriptコンパイラの最重要オプション、--noImplicitAnyを使っ
964964@< list> {noImplicitAny/ basic- invalid}のような、メソッドの返り値の型を書き忘れた!という脇の甘いコードをコンパイルしてみます。
965965
966966// list[noImplicitAny/basic-invalid][メソッドの返り値を書き忘れた!]{
967- #@ mapfile(../ code- 2 . 0 / definition- file/ noImplicitAny/ basic- invalid. d. ts)
967+ #@ mapfile(../ code/ definition- file/ noImplicitAny/ basic- invalid. d. ts)
968968declare class Sample {
969969 // 返り値の型を指定し忘れている!
970970 // error TS7010: 'method', which lacks return-type annotation,
0 commit comments