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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
//// [mappedTypeErrors.ts]
interface Shape {
name: string;
width: number;
height: number;
visible: boolean;
}
interface Named {
name: string;
}
interface Point {
x: number;
y: number;
}
// Constraint checking
type T00 = { [P in P]: string }; // Error
type T01 = { [P in number]: string }; // Error
type T02 = { [P in Date]: number }; // Error
type T03 = Record<Date, number>; // Error
type T10 = Pick<Shape, "name">;
type T11 = Pick<Shape, "foo">; // Error
type T12 = Pick<Shape, "name" | "foo">; // Error
type T13 = Pick<Shape, keyof Named>;
type T14 = Pick<Shape, keyof Point>; // Error
type T15 = Pick<Shape, never>;
type T16 = Pick<Shape, undefined>; // Error
function f1<T>(x: T) {
let y: Pick<Shape, T>; // Error
}
function f2<T extends string | number>(x: T) {
let y: Pick<Shape, T>; // Error
}
function f3<T extends keyof Shape>(x: T) {
let y: Pick<Shape, T>;
}
function f4<T extends keyof Named>(x: T) {
let y: Pick<Shape, T>;
}
// Type identity checking
function f10<T>() {
type K = keyof T;
var x: { [P in keyof T]: T[P] };
var x: { [Q in keyof T]: T[Q] };
var x: { [R in K]: T[R] };
}
function f11<T>() {
var x: { [P in keyof T]: T[P] };
var x: { [P in keyof T]?: T[P] }; // Error
var x: { readonly [P in keyof T]: T[P] }; // Error
var x: { readonly [P in keyof T]?: T[P] }; // Error
}
function f12<T>() {
var x: { [P in keyof T]: T[P] };
var x: { [P in keyof T]: T[P][] }; // Error
}
// Check that inferences to mapped types are secondary
declare function objAndReadonly<T>(primary: T, secondary: Readonly<T>): T;
declare function objAndPartial<T>(primary: T, secondary: Partial<T>): T;
function f20() {
let x1 = objAndReadonly({ x: 0, y: 0 }, { x: 1 }); // Error
let x2 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1 });
let x3 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error
}
function f21() {
let x1 = objAndPartial({ x: 0, y: 0 }, { x: 1 });
let x2 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1 });
let x3 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error
}
// Verify use of Pick<T, K> for setState functions (#12793)
interface Foo {
a: string;
b?: number;
}
function setState<T, K extends keyof T>(obj: T, props: Pick<T, K>) {
for (let k in props) {
obj[k] = props[k];
}
}
let foo: Foo = { a: "hello", b: 42 };
setState(foo, { a: "test", b: 43 })
setState(foo, { a: "hi" });
setState(foo, { b: undefined });
setState(foo, { });
setState(foo, foo);
setState(foo, { a: undefined }); // Error
setState(foo, { c: true }); // Error
class C<T> {
state: T;
setState<K extends keyof T>(props: Pick<T, K>) {
for (let k in props) {
this.state[k] = props[k];
}
}
}
let c = new C<Foo>();
c.setState({ a: "test", b: 43 });
c.setState({ a: "hi" });
c.setState({ b: undefined });
c.setState({ });
c.setState(foo);
c.setState({ a: undefined }); // Error
c.setState({ c: true }); // Error
type T2 = { a?: number, [key: string]: any };
let x1: T2 = { a: 'no' }; // Error
let x2: Partial<T2> = { a: 'no' }; // Error
let x3: { [P in keyof T2]: T2[P]} = { a: 'no' }; // Error
// Repro from #13044
type Foo2<T, F extends keyof T> = {
pf: {[P in F]?: T[P]},
pt: {[P in T]?: T[P]}, // note: should be in keyof T
};
type O = {x: number, y: boolean};
let o: O = {x: 5, y: false};
let f: Foo2<O, 'x'> = {
pf: {x: 7},
pt: {x: 7, y: false},
};
//// [mappedTypeErrors.js]
function f1(x) {
var y; // Error
}
function f2(x) {
var y; // Error
}
function f3(x) {
var y;
}
function f4(x) {
var y;
}
// Type identity checking
function f10() {
var x;
var x;
var x;
}
function f11() {
var x;
var x; // Error
var x; // Error
var x; // Error
}
function f12() {
var x;
var x; // Error
}
function f20() {
var x1 = objAndReadonly({ x: 0, y: 0 }, { x: 1 }); // Error
var x2 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1 });
var x3 = objAndReadonly({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error
}
function f21() {
var x1 = objAndPartial({ x: 0, y: 0 }, { x: 1 });
var x2 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1 });
var x3 = objAndPartial({ x: 0, y: 0 }, { x: 1, y: 1, z: 1 }); // Error
}
function setState(obj, props) {
for (var k in props) {
obj[k] = props[k];
}
}
var foo = { a: "hello", b: 42 };
setState(foo, { a: "test", b: 43 });
setState(foo, { a: "hi" });
setState(foo, { b: undefined });
setState(foo, {});
setState(foo, foo);
setState(foo, { a: undefined }); // Error
setState(foo, { c: true }); // Error
var C = (function () {
function C() {
}
C.prototype.setState = function (props) {
for (var k in props) {
this.state[k] = props[k];
}
};
return C;
}());
var c = new C();
c.setState({ a: "test", b: 43 });
c.setState({ a: "hi" });
c.setState({ b: undefined });
c.setState({});
c.setState(foo);
c.setState({ a: undefined }); // Error
c.setState({ c: true }); // Error
var x1 = { a: 'no' }; // Error
var x2 = { a: 'no' }; // Error
var x3 = { a: 'no' }; // Error
var o = { x: 5, y: false };
var f = {
pf: { x: 7 },
pt: { x: 7, y: false }
};
//// [mappedTypeErrors.d.ts]
interface Shape {
name: string;
width: number;
height: number;
visible: boolean;
}
interface Named {
name: string;
}
interface Point {
x: number;
y: number;
}
declare type T00 = {
[P in P]: string;
};
declare type T01 = {
[P in number]: string;
};
declare type T02 = {
[P in Date]: number;
};
declare type T03 = Record<Date, number>;
declare type T10 = Pick<Shape, "name">;
declare type T11 = Pick<Shape, "foo">;
declare type T12 = Pick<Shape, "name" | "foo">;
declare type T13 = Pick<Shape, keyof Named>;
declare type T14 = Pick<Shape, keyof Point>;
declare type T15 = Pick<Shape, never>;
declare type T16 = Pick<Shape, undefined>;
declare function f1<T>(x: T): void;
declare function f2<T extends string | number>(x: T): void;
declare function f3<T extends keyof Shape>(x: T): void;
declare function f4<T extends keyof Named>(x: T): void;
declare function f10<T>(): void;
declare function f11<T>(): void;
declare function f12<T>(): void;
declare function objAndReadonly<T>(primary: T, secondary: Readonly<T>): T;
declare function objAndPartial<T>(primary: T, secondary: Partial<T>): T;
declare function f20(): void;
declare function f21(): void;
interface Foo {
a: string;
b?: number;
}
declare function setState<T, K extends keyof T>(obj: T, props: Pick<T, K>): void;
declare let foo: Foo;
declare class C<T> {
state: T;
setState<K extends keyof T>(props: Pick<T, K>): void;
}
declare let c: C<Foo>;
declare type T2 = {
a?: number;
[key: string]: any;
};
declare let x1: T2;
declare let x2: Partial<T2>;
declare let x3: {
[P in keyof T2]: T2[P];
};
declare type Foo2<T, F extends keyof T> = {
pf: {
[P in F]?: T[P];
};
pt: {
[P in T]?: T[P];
};
};
declare type O = {
x: number;
y: boolean;
};
declare let o: O;
declare let f: Foo2<O, 'x'>;
|