|
| 1 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(116,37): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 2 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(117,37): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 3 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(118,37): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 4 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(121,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 5 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(122,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 6 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(123,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 7 | +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(124,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 8 | + |
| 9 | + |
| 10 | +==== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts (7 errors) ==== |
| 11 | + declare function isString(value: unknown): value is string; |
| 12 | + declare function isArrayOfStrings(value: unknown): value is string[]; |
| 13 | + |
| 14 | + const assert: (value: unknown) => asserts value = value => {} |
| 15 | + |
| 16 | + declare function assertIsString(value: unknown): asserts value is string; |
| 17 | + declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; |
| 18 | + declare function assertDefined<T>(value: T): asserts value is NonNullable<T>; |
| 19 | + |
| 20 | + function f01(x: unknown) { |
| 21 | + if (!!true) { |
| 22 | + assert(typeof x === "string"); |
| 23 | + x.length; |
| 24 | + } |
| 25 | + if (!!true) { |
| 26 | + assert(x instanceof Error); |
| 27 | + x.message; |
| 28 | + } |
| 29 | + if (!!true) { |
| 30 | + assert(typeof x === "boolean" || typeof x === "number"); |
| 31 | + x.toLocaleString; |
| 32 | + } |
| 33 | + if (!!true) { |
| 34 | + assert(isArrayOfStrings(x)); |
| 35 | + x[0].length; |
| 36 | + } |
| 37 | + if (!!true) { |
| 38 | + assertIsArrayOfStrings(x); |
| 39 | + x[0].length; |
| 40 | + } |
| 41 | + if (!!true) { |
| 42 | + assert(x === undefined || typeof x === "string"); |
| 43 | + x; // string | undefined |
| 44 | + assertDefined(x); |
| 45 | + x; // string |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + function f02(x: string | undefined) { |
| 50 | + if (!!true) { |
| 51 | + assert(x); |
| 52 | + x.length; |
| 53 | + } |
| 54 | + if (!!true) { |
| 55 | + assert(x !== undefined); |
| 56 | + x.length; |
| 57 | + } |
| 58 | + if (!!true) { |
| 59 | + assertDefined(x); |
| 60 | + x.length; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function f03(x: string | undefined, assert: (value: unknown) => asserts value) { |
| 65 | + assert(x); |
| 66 | + x.length; |
| 67 | + } |
| 68 | + |
| 69 | + namespace Debug { |
| 70 | + export declare function assert(value: unknown, message?: string): asserts value; |
| 71 | + export declare function assertDefined<T>(value: T): asserts value is NonNullable<T>; |
| 72 | + } |
| 73 | + |
| 74 | + function f10(x: string | undefined) { |
| 75 | + if (!!true) { |
| 76 | + Debug.assert(x); |
| 77 | + x.length; |
| 78 | + } |
| 79 | + if (!!true) { |
| 80 | + Debug.assert(x !== undefined); |
| 81 | + x.length; |
| 82 | + } |
| 83 | + if (!!true) { |
| 84 | + Debug.assertDefined(x); |
| 85 | + x.length; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + class Test { |
| 90 | + assert(value: unknown): asserts value { |
| 91 | + if (value) return; |
| 92 | + throw new Error(); |
| 93 | + } |
| 94 | + isTest2(): this is Test2 { |
| 95 | + return this instanceof Test2; |
| 96 | + } |
| 97 | + assertIsTest2(): asserts this is Test2 { |
| 98 | + if (this instanceof Test2) return; |
| 99 | + throw new Error(); |
| 100 | + } |
| 101 | + assertThis(): asserts this { |
| 102 | + if (!this) return; |
| 103 | + throw new Error(); |
| 104 | + } |
| 105 | + bar() { |
| 106 | + this.assertThis(); |
| 107 | + this; |
| 108 | + } |
| 109 | + foo(x: unknown) { |
| 110 | + this.assert(typeof x === "string"); |
| 111 | + x.length; |
| 112 | + if (this.isTest2()) { |
| 113 | + this.z; |
| 114 | + } |
| 115 | + this.assertIsTest2(); |
| 116 | + this.z; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + class Test2 extends Test { |
| 121 | + z = 0; |
| 122 | + } |
| 123 | + |
| 124 | + // Invalid constructs |
| 125 | + |
| 126 | + declare let Q1: new (x: unknown) => x is string; |
| 127 | + ~~~~~~~~~~~ |
| 128 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 129 | + declare let Q2: new (x: boolean) => asserts x; |
| 130 | + ~~~~~~~~~ |
| 131 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 132 | + declare let Q3: new (x: unknown) => asserts x is string; |
| 133 | + ~~~~~~~~~~~~~~~~~~~ |
| 134 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 135 | + |
| 136 | + declare class Wat { |
| 137 | + get p1(): this is string; |
| 138 | + ~~~~~~~~~~~~~~ |
| 139 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 140 | + set p1(x: this is string); |
| 141 | + ~~~~~~~~~~~~~~ |
| 142 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 143 | + get p2(): asserts this is string; |
| 144 | + ~~~~~~~~~~~~~~~~~~~~~~ |
| 145 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 146 | + set p2(x: asserts this is string); |
| 147 | + ~~~~~~~~~~~~~~~~~~~~~~ |
| 148 | +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. |
| 149 | + } |
| 150 | + |
0 commit comments