@@ -601,7 +601,7 @@ class Sample {
601
601
602
602
// 構造的部分型!
603
603
let obj: Sample = {
604
- str: "Hi!"
604
+ str: "Hi!" ,
605
605
};
606
606
607
607
if (obj instanceof Sample ) {
@@ -625,7 +625,7 @@ class Sample {
625
625
}
626
626
// 構造的部分型!
627
627
let obj = {
628
- str: "Hi!"
628
+ str: "Hi!" ,
629
629
};
630
630
if (obj instanceof Sample ) {
631
631
// 型はSampleに絞られている しかし、絶対に到達しない
@@ -647,17 +647,17 @@ class Sample {
647
647
}
648
648
649
649
// 構造的部分型!
650
- var obj: Sample = {
651
- str: "Hi!"
650
+ let obj: Sample = {
651
+ str: "Hi!" ,
652
652
};
653
653
654
654
// 独自にSample型である事の判定を実装する
655
- function isSample(obj : Sample ): obj is Sample {
656
- if (! obj ) {
655
+ function isSample(s : Sample ): s is Sample {
656
+ if (! s ) {
657
657
return false ;
658
658
}
659
659
// とりあえず、strプロパティがあって値がstringならSample型コンパチということでOK という基準にする
660
- return typeof obj . str === "string" ;
660
+ return typeof s . str === "string" ;
661
661
}
662
662
663
663
if (isSample(obj)) {
@@ -674,17 +674,17 @@ export { }
674
674
// list[typeGuards/vsWeakspot2-invalid][privateな要素があれば構造的部分型で値を偽造できない]{
675
675
#@ mapfile(../ code/ types- advanced/ typeGuards/ vsWeakspot2- invalid. ts)
676
676
class Sample {
677
- private _tmp: any;
678
677
str: string;
678
+ private _tmp: any;
679
679
}
680
680
681
681
// privateなインスタンス変数があるクラスのインスタンスは偽造できない!
682
682
// error TS2322: Type '{ _tmp: null; str: string; }' is not
683
683
// assignable to type 'Sample'. Property '_tmp' is private
684
684
// in type 'Sample' but not in type '{ _tmp: null; str: string; }'.
685
685
let obj: Sample = {
686
- _tmp: null,
687
686
str: "Hi!" ,
687
+ _tmp: null,
688
688
};
689
689
#@ end
690
690
// }
@@ -744,8 +744,8 @@ namespace alternative {
744
744
constructor(public p: Point , public r: number) {
745
745
}
746
746
}
747
- let c : Circle = new Circle ( new Point ( 1, 2) , 3) ;
748
- console. log(c . p, c . r);
747
+ let c2 : Circle = new Circle ( new Point ( 1, 2) , 3) ;
748
+ console. log(c2 . p, c2 . r);
749
749
}
750
750
751
751
export { c, alternative }
0 commit comments