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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts(44,5): error TS2322: Type 'S' is not assignable to type 'T'.
Type 'S' is not assignable to type '{ a: 2; b: 3; }'.
Types of property 'a' are incompatible.
Type '0 | 2' is not assignable to type '2'.
Type '0' is not assignable to type '2'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts(58,5): error TS2322: Type 'S' is not assignable to type 'T'.
Property 'c' is missing in type 'S' but required in type '{ a: 2; b: 4 | 3; c: string; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts(82,5): error TS2322: Type 'S' is not assignable to type 'T'.
Type 'S' is not assignable to type '{ a: N; b: N; c: 2; }'.
Types of property 'c' are incompatible.
Type 'N' is not assignable to type '2'.
Type '0' is not assignable to type '2'.
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts (3 errors) ====
// see 'typeRelatedToDiscriminatedType' in checker.ts:
// IteratorResult
namespace Example1 {
type S = { done: boolean, value: number };
type T =
| { done: true, value: number } // T0
| { done: false, value: number }; // T1
declare let s: S;
declare let t: T;
// S is assignable to T0 when S["done"] is true
// S is assignable to T1 when S["done"] is false
t = s;
}
// Dropping constituents of T
namespace Example2 {
type S = { a: 0 | 2, b: 4 };
type T = { a: 0, b: 1 | 4 } // T0
| { a: 1, b: 2 } // T1
| { a: 2, b: 3 | 4 }; // T2
declare let s: S;
declare let t: T;
// S is assignable to T0 when S["a"] is 0
// S is assignable to T2 when S["a"] is 2
t = s;
}
// Unmatched discriminants
namespace Example3 {
type S = { a: 0 | 2, b: 4 };
type T = { a: 0, b: 1 | 4 } // T0
| { a: 1, b: 2 | 4 } // T1
| { a: 2, b: 3 }; // T2
declare let s: S;
declare let t: T;
// S is assignable to T0 when S["a"] is 0
// S is *not* assignable to T1 when S["b"] is 4
// S is *not* assignable to T2 when S["a"] is 2
t = s;
~
!!! error TS2322: Type 'S' is not assignable to type 'T'.
!!! error TS2322: Type 'S' is not assignable to type '{ a: 2; b: 3; }'.
!!! error TS2322: Types of property 'a' are incompatible.
!!! error TS2322: Type '0 | 2' is not assignable to type '2'.
!!! error TS2322: Type '0' is not assignable to type '2'.
}
// Unmatched non-discriminants
namespace Example4 {
type S = { a: 0 | 2, b: 4 };
type T = { a: 0, b: 1 | 4 } // T0
| { a: 1, b: 2 } // T1
| { a: 2, b: 3 | 4, c: string }; // T2
declare let s: S;
declare let t: T;
// S is assignable to T0 when S["a"] is 0
// S is *not* assignable to T2 when S["a"] is 2 as S is missing "c"
t = s;
~
!!! error TS2322: Type 'S' is not assignable to type 'T'.
!!! error TS2322: Property 'c' is missing in type 'S' but required in type '{ a: 2; b: 4 | 3; c: string; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithDiscriminatedUnion.ts:52:36: 'c' is declared here.
}
// Maximum discriminant combinations
namespace Example5 {
// NOTE: The maximum number of discriminant type combinations is currently 25.
// 3 discriminant properties with 3 types a piece
// is 27 possible combinations.
type N = 0 | 1 | 2;
type S = { a: N, b: N, c: N };
type T = { a: 0, b: N, c: N }
| { a: 1, b: N, c: N }
| { a: 2, b: N, c: N }
| { a: N, b: 0, c: N }
| { a: N, b: 1, c: N }
| { a: N, b: 2, c: N }
| { a: N, b: N, c: 0 }
| { a: N, b: N, c: 1 }
| { a: N, b: N, c: 2 };
declare let s: S;
declare let t: T;
// S *should* be assignable but the number of
// combinations is too complex.
t = s;
~
!!! error TS2322: Type 'S' is not assignable to type 'T'.
!!! error TS2322: Type 'S' is not assignable to type '{ a: N; b: N; c: 2; }'.
!!! error TS2322: Types of property 'c' are incompatible.
!!! error TS2322: Type 'N' is not assignable to type '2'.
!!! error TS2322: Type '0' is not assignable to type '2'.
}
// https://github.com/Microsoft/TypeScript/issues/14865
namespace GH14865 {
type Style1 = {
type: "A";
data: string;
} | {
type: "B";
data: string;
};
type Style2 = {
type: "A" | "B";
data: string;
}
const a: Style2 = { type: "A", data: "whatevs" };
let b: Style1;
a.type; // "A" | "B"
b.type; // "A" | "B"
b = a; // should be assignable
}
// https://github.com/Microsoft/TypeScript/issues/30170
namespace GH30170 {
interface Blue {
color: 'blue'
}
interface Yellow {
color?: 'yellow',
}
function draw(val: Blue | Yellow) { }
function drawWithColor(currentColor: 'blue' | 'yellow' | undefined) {
return draw({ color: currentColor });
}
}
// https://github.com/Microsoft/TypeScript/issues/12052
namespace GH12052 {
interface ILinearAxis { type: "linear"; }
interface ICategoricalAxis { type: "categorical"; }
type IAxis = ILinearAxis | ICategoricalAxis;
type IAxisType = "linear" | "categorical";
function getAxisType(): IAxisType {
if (1 == 1) {
return "categorical";
} else {
return "linear";
}
}
const bad: IAxis = { type: getAxisType() };
const good: IAxis = { type: undefined };
good.type = getAxisType();
}
// https://github.com/Microsoft/TypeScript/issues/18421
namespace GH18421 {
interface ThingTypeOne {
type: 'one';
}
interface ThingTypeTwo {
type: 'two';
}
type ThingType = 'one' | 'two';
type Thing = ThingTypeOne | ThingTypeTwo;
function makeNewThing(thingType: ThingType): Thing {
return {
type: thingType
};
}
}
// https://github.com/Microsoft/TypeScript/issues/15907
namespace GH15907 {
type Action = { type: 'activate' } | { type: 'disactivate' };
function dispatchAction(action: Action): void {
}
const active = true;
dispatchAction({ type : (active? 'disactivate' : 'activate') });
}
// https://github.com/Microsoft/TypeScript/issues/20889
namespace GH20889 {
interface A1 {
type: "A1";
}
interface A2 {
type: "A2";
}
type AU = A1 | A2;
function foo(obj1: AU) {
const obj2: AU = {
type: obj1.type
};
}
}
// https://github.com/microsoft/TypeScript/issues/39357
namespace GH39357 {
type A = ["a", number] | ["b", number] | ["c", string];
type B = "a" | "b" | "c";
declare const b: B;
const a: A = b === "a" || b === "b" ? [b, 1] : ["c", ""];
}
|