1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
//// [typeGuardNarrowsIndexedAccessOfKnownProperty.ts]
interface Square {
["dash-ok"]: "square";
["square-size"]: number;
}
interface Rectangle {
["dash-ok"]: "rectangle";
width: number;
height: number;
}
interface Circle {
["dash-ok"]: "circle";
radius: number;
}
type Shape = Square | Rectangle | Circle;
interface Subshape {
"0": {
sub: {
under: {
shape: Shape;
}
}
}
}
function area(s: Shape): number {
switch(s['dash-ok']) {
case "square": return s['square-size'] * s['square-size'];
case "rectangle": return s.width * s['height'];
case "circle": return Math.PI * s['radius'] * s.radius;
}
}
function subarea(s: Subshape): number {
switch(s[0]["sub"].under["shape"]["dash-ok"]) {
case "square": return s[0].sub.under.shape["square-size"] * s[0].sub.under.shape["square-size"];
case "rectangle": return s[0]["sub"]["under"]["shape"]["width"] * s[0]["sub"]["under"]["shape"].height;
case "circle": return Math.PI * s[0].sub.under["shape"].radius * s[0]["sub"].under.shape["radius"];
}
}
interface X {
0: "xx",
1: number
}
interface Y {
0: "yy",
1: string
}
type A = ["aa", number];
type B = ["bb", string];
type Z = X | Y;
type C = A | B;
function check(z: Z, c: C) {
z[0] // fine, typescript sees "xx" | "yy"
switch (z[0]) {
case "xx":
var xx: number = z[1] // should be number
break;
case "yy":
var yy: string = z[1] // should be string
break;
}
c[0] // fine, typescript sees "xx" | "yy"
switch (c[0]) {
case "aa":
var aa: number = c[1] // should be number
break;
case "bb":
var bb: string = c[1] // should be string
break;
}
}
export function g(pair: [number, string?]): string {
return pair[1] ? pair[1] : 'nope';
}
//// [typeGuardNarrowsIndexedAccessOfKnownProperty.js]
"use strict";
exports.__esModule = true;
function area(s) {
switch (s['dash-ok']) {
case "square": return s['square-size'] * s['square-size'];
case "rectangle": return s.width * s['height'];
case "circle": return Math.PI * s['radius'] * s.radius;
}
}
function subarea(s) {
switch (s[0]["sub"].under["shape"]["dash-ok"]) {
case "square": return s[0].sub.under.shape["square-size"] * s[0].sub.under.shape["square-size"];
case "rectangle": return s[0]["sub"]["under"]["shape"]["width"] * s[0]["sub"]["under"]["shape"].height;
case "circle": return Math.PI * s[0].sub.under["shape"].radius * s[0]["sub"].under.shape["radius"];
}
}
function check(z, c) {
z[0]; // fine, typescript sees "xx" | "yy"
switch (z[0]) {
case "xx":
var xx = z[1]; // should be number
break;
case "yy":
var yy = z[1]; // should be string
break;
}
c[0]; // fine, typescript sees "xx" | "yy"
switch (c[0]) {
case "aa":
var aa = c[1]; // should be number
break;
case "bb":
var bb = c[1]; // should be string
break;
}
}
function g(pair) {
return pair[1] ? pair[1] : 'nope';
}
exports.g = g;
|