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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
//// [indexSignatures1.ts]
// Symbol index signature checking
const sym = Symbol();
function gg3(x: { [key: string]: string }, y: { [key: symbol]: string }, z: { [sym]: number }) {
x = z;
y = z; // Error
}
// Overlapping index signatures
function gg1(x: { [key: `a${string}`]: string, [key: `${string}a`]: string }, y: { [key: `a${string}a`]: string }) {
x = y;
y = x;
}
interface IX { [key: `a${string}`]: string, [key: `${string}a`]: string }
interface IY { [key: `a${string}a`]: string }
function gg2(x: IX, y: IY) {
x = y; // Error
y = x;
}
// Intersection of multiple applicable index signatures
declare let combo: { [x: `foo-${string}`]: 'a' | 'b' } & { [x: `${string}-bar`]: 'b' | 'c' };
const x1 = combo['foo-test']; // 'a' | 'b'
const x2 = combo['test-bar']; // 'b' | 'c'
const x3 = combo['foo-test-bar']; // 'b' (('a' | 'b') & ('b' | 'c'))
declare var str: string;
const x4 = combo[`foo-${str}`];
const x5 = combo[`${str}-bar`];
const x6 = combo[`foo-${str}-bar`];
declare let combo2: { [x: `${string}xxx${string}` & `${string}yyy${string}`]: string };
const x7 = combo2['axxxbyyyc'];
const x8 = combo2['ayyyxxxbc'];
const x9 = combo2['axxxbbbyc']; // Error
// Property access on template pattern index signature
declare let dom: { [x: `data${string}`]: string };
const y1 = dom['data123'];
const y2 = dom.data123;
// Excess property checking for template pattern index signature
dom = { data123: 'hello' };
dom = { date123: 'hello' }; // Error
// Contextual typing by index signature with template literal pattern
type Funcs = {
[key: `s${string}`]: (x: string) => void,
[key: `n${string}`]: (x: number) => void,
}
const funcs: Funcs = {
sfoo: x => x.length, // x: string
nfoo: x => x * 2, // n: number
}
// Duplicate index signature checking
type Duplicates = {
[key: string | number]: any; // Error
[key: number | symbol]: any; // Error
[key: symbol | `foo${string}`]: any; // Error
[key: `foo${string}`]: any; // Error
}
// Conflicting index signature checking
type Conflicting = {
[key: `a${string}`]: 'a';
[key: `${string}a`]: 'b';
[key: `a${string}a`]: 'c'; // Error
}
// Invalid index signatures
type Invalid<T extends string> = {
[key: 'a' | 'b' | 'c']: string; // Error
[key: T | number]: string; // Error
[key: Error]: string; // Error
[key: T & string]: string; // Error
}
// Intersections in index signatures
type Tag1 = { __tag1__: void };
type Tag2 = { __tag2__: void };
type TaggedString1 = string & Tag1;
type TaggedString2 = string & Tag2;
declare let s0: string;
declare let s1: TaggedString1;
declare let s2: TaggedString2;
declare let s3: TaggedString1 | TaggedString2;
declare let s4: TaggedString1 & TaggedString2;
interface I1 { [key: TaggedString1]: string }
interface I2 { [key: TaggedString2]: string }
interface I3 { [key: TaggedString1 | TaggedString2]: string }
interface I4 { [key: TaggedString1 & TaggedString2]: string }
declare let i1: I1;
declare let i2: I2;
declare let i3: I3;
declare let i4: I4;
i1[s0]; // Error
i1[s1];
i1[s2]; // Error
i1[s3]; // Error
i1[s4];
i2[s0]; // Error
i2[s1]; // Error
i2[s2];
i2[s3]; // Error
i2[s4];
i3[s0]; // Error
i3[s1];
i3[s2];
i3[s3];
i3[s4];
i4[s0]; // Error
i4[s1]; // Error
i4[s2]; // Error
i4[s3]; // Error
i4[s4];
i1 = i2; // Error
i1 = i3;
i1 = i4; // Error
i2 = i1; // Error
i2 = i3;
i2 = i4; // Error
i3 = i1; // Error
i3 = i2; // Error
i3 = i4; // Error
i4 = i1;
i4 = i2;
i4 = i3;
declare let o1: { [key: TaggedString1]: string };
declare let o2: { [key: TaggedString2]: string };
declare let o3: { [key: TaggedString1 | TaggedString2]: string };
declare let o4: { [key: TaggedString1 & TaggedString2]: string };
o1[s0]; // Error
o1[s1];
o1[s2]; // Error
o1[s3]; // Error
o1[s4];
o2[s0]; // Error
o2[s1]; // Error
o2[s2];
o2[s3]; // Error
o2[s4];
o3[s0]; // Error
o3[s1];
o3[s2];
o3[s3];
o3[s4];
o4[s0]; // Error
o4[s1]; // Error
o4[s2]; // Error
o4[s3]; // Error
o4[s4];
o1 = o2;
o1 = o3;
o1 = o4;
o2 = o1;
o2 = o3;
o2 = o4;
o3 = o1;
o3 = o2;
o3 = o4;
o4 = o1;
o4 = o2;
o4 = o3;
// Index signatures inferred from computed property names
const obj10 = {
['x']: 0 as const,
['a' + 'b']: 1 as const,
};
const obj11 = {
[1]: 2 as const,
[1 + 2]: 3 as const,
};
const obj12 = {
[sym]: 4 as const,
[Symbol()]: 5 as const,
};
const obj13 = {
['x']: 0 as const,
['a' + 'b']: 1 as const,
[1]: 2 as const,
[1 + 2]: 3 as const,
[sym]: 4 as const,
[Symbol()]: 5 as const,
};
// Repros from #1863
const system = Symbol('system');
const SomeSytePlugin = Symbol('SomeSytePlugin');
interface Plugs {
[key: symbol]: (...args: any) => unknown;
}
const plugins = {
"user": {} as Plugs,
[system]: {} as Plugs
};
plugins[system][SomeSytePlugin] = () => console.log('awsome');
plugins[system][SomeSytePlugin]();
var theAnswer: symbol = Symbol('secret');
var obj = {} as Record<symbol, number>;
obj[theAnswer] = 42;
// Repro from #26470
const directive = Symbol('directive');
declare function foo<TArg, TRet, TDir>(options: { [x in string]: (arg: TArg) => TRet } & { [directive]?: TDir }): void;
let case1 = foo({
[directive]: (x: string) => 'str',
addOne: (x: number) => x + 1,
double: (x: number) => x + x,
});
let case2 = foo({
addOne: (x: number) => x + 1,
double: (x: number) => x + x,
[directive]: (x: string) => 'str',
});
let case3 = foo({
[directive]: 'str',
addOne: (x: number) => x + 1,
double: (x: number) => x + x,
});
// Repros from #42192
type Pseudo = `&:${string}`;
const AmIPseudo1: Pseudo = '&:test';
const AmIPseudo: Pseudo = '&'; // Error
type PseudoDeclaration = { [key in Pseudo]: string };
const test: PseudoDeclaration = { 'someKey' : 'someValue' }; // Error
type FieldPattern = `/${string}`;
const path1: FieldPattern = '/one';
const path2: FieldPattern = 'two'; // Error
type PathsObject = { [P in FieldPattern]: object; };
const pathObject: PathsObject = 123; // Error
type IdType = `${number}-${number}-${number}-${number}`
const id: IdType = '0000-0000-0000-0001';
type A = Record<IdType, string>;
const a: A = { [id]: 'test' }
let aid = a[id];
// Repro from #44793
interface AA {
a?: string;
b?: number;
[key: symbol]: string;
}
const aa: AA = { [sym]: '123' };
const obj1: { [key: symbol]: string } = { [sym]: 'hello '};
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Permitted for backwards compatibility
const obj3: { [key: number]: string } = { [sym]: 'hello '}; // Error
// Repro from #45772
type Id = string & { __tag: 'id '};
type Rec1 = { [key: Id]: number };
type Rec2 = Record<Id, number>;
type K1 = keyof Rec1; // Id
type K2 = keyof Rec2; // Id
//// [indexSignatures1.js]
"use strict";
// Symbol index signature checking
const sym = Symbol();
function gg3(x, y, z) {
x = z;
y = z; // Error
}
// Overlapping index signatures
function gg1(x, y) {
x = y;
y = x;
}
function gg2(x, y) {
x = y; // Error
y = x;
}
const x1 = combo['foo-test']; // 'a' | 'b'
const x2 = combo['test-bar']; // 'b' | 'c'
const x3 = combo['foo-test-bar']; // 'b' (('a' | 'b') & ('b' | 'c'))
const x4 = combo[`foo-${str}`];
const x5 = combo[`${str}-bar`];
const x6 = combo[`foo-${str}-bar`];
const x7 = combo2['axxxbyyyc'];
const x8 = combo2['ayyyxxxbc'];
const x9 = combo2['axxxbbbyc']; // Error
const y1 = dom['data123'];
const y2 = dom.data123;
// Excess property checking for template pattern index signature
dom = { data123: 'hello' };
dom = { date123: 'hello' }; // Error
const funcs = {
sfoo: x => x.length,
nfoo: x => x * 2, // n: number
};
i1[s0]; // Error
i1[s1];
i1[s2]; // Error
i1[s3]; // Error
i1[s4];
i2[s0]; // Error
i2[s1]; // Error
i2[s2];
i2[s3]; // Error
i2[s4];
i3[s0]; // Error
i3[s1];
i3[s2];
i3[s3];
i3[s4];
i4[s0]; // Error
i4[s1]; // Error
i4[s2]; // Error
i4[s3]; // Error
i4[s4];
i1 = i2; // Error
i1 = i3;
i1 = i4; // Error
i2 = i1; // Error
i2 = i3;
i2 = i4; // Error
i3 = i1; // Error
i3 = i2; // Error
i3 = i4; // Error
i4 = i1;
i4 = i2;
i4 = i3;
o1[s0]; // Error
o1[s1];
o1[s2]; // Error
o1[s3]; // Error
o1[s4];
o2[s0]; // Error
o2[s1]; // Error
o2[s2];
o2[s3]; // Error
o2[s4];
o3[s0]; // Error
o3[s1];
o3[s2];
o3[s3];
o3[s4];
o4[s0]; // Error
o4[s1]; // Error
o4[s2]; // Error
o4[s3]; // Error
o4[s4];
o1 = o2;
o1 = o3;
o1 = o4;
o2 = o1;
o2 = o3;
o2 = o4;
o3 = o1;
o3 = o2;
o3 = o4;
o4 = o1;
o4 = o2;
o4 = o3;
// Index signatures inferred from computed property names
const obj10 = {
['x']: 0,
['a' + 'b']: 1,
};
const obj11 = {
[1]: 2,
[1 + 2]: 3,
};
const obj12 = {
[sym]: 4,
[Symbol()]: 5,
};
const obj13 = {
['x']: 0,
['a' + 'b']: 1,
[1]: 2,
[1 + 2]: 3,
[sym]: 4,
[Symbol()]: 5,
};
// Repros from #1863
const system = Symbol('system');
const SomeSytePlugin = Symbol('SomeSytePlugin');
const plugins = {
"user": {},
[system]: {}
};
plugins[system][SomeSytePlugin] = () => console.log('awsome');
plugins[system][SomeSytePlugin]();
var theAnswer = Symbol('secret');
var obj = {};
obj[theAnswer] = 42;
// Repro from #26470
const directive = Symbol('directive');
let case1 = foo({
[directive]: (x) => 'str',
addOne: (x) => x + 1,
double: (x) => x + x,
});
let case2 = foo({
addOne: (x) => x + 1,
double: (x) => x + x,
[directive]: (x) => 'str',
});
let case3 = foo({
[directive]: 'str',
addOne: (x) => x + 1,
double: (x) => x + x,
});
const AmIPseudo1 = '&:test';
const AmIPseudo = '&'; // Error
const test = { 'someKey': 'someValue' }; // Error
const path1 = '/one';
const path2 = 'two'; // Error
const pathObject = 123; // Error
const id = '0000-0000-0000-0001';
const a = { [id]: 'test' };
let aid = a[id];
const aa = { [sym]: '123' };
const obj1 = { [sym]: 'hello ' };
const obj2 = { [sym]: 'hello ' }; // Permitted for backwards compatibility
const obj3 = { [sym]: 'hello ' }; // Error
//// [indexSignatures1.d.ts]
declare const sym: unique symbol;
declare function gg3(x: {
[key: string]: string;
}, y: {
[key: symbol]: string;
}, z: {
[sym]: number;
}): void;
declare function gg1(x: {
[key: `a${string}`]: string;
[key: `${string}a`]: string;
}, y: {
[key: `a${string}a`]: string;
}): void;
interface IX {
[key: `a${string}`]: string;
[key: `${string}a`]: string;
}
interface IY {
[key: `a${string}a`]: string;
}
declare function gg2(x: IX, y: IY): void;
declare let combo: {
[x: `foo-${string}`]: 'a' | 'b';
} & {
[x: `${string}-bar`]: 'b' | 'c';
};
declare const x1: "a" | "b";
declare const x2: "b" | "c";
declare const x3: "b";
declare var str: string;
declare const x4: "a" | "b";
declare const x5: "b" | "c";
declare const x6: "b";
declare let combo2: {
[x: `${string}xxx${string}` & `${string}yyy${string}`]: string;
};
declare const x7: string;
declare const x8: string;
declare const x9: any;
declare let dom: {
[x: `data${string}`]: string;
};
declare const y1: string;
declare const y2: string;
type Funcs = {
[key: `s${string}`]: (x: string) => void;
[key: `n${string}`]: (x: number) => void;
};
declare const funcs: Funcs;
type Duplicates = {
[key: string | number]: any;
[key: number | symbol]: any;
[key: symbol | `foo${string}`]: any;
[key: `foo${string}`]: any;
};
type Conflicting = {
[key: `a${string}`]: 'a';
[key: `${string}a`]: 'b';
[key: `a${string}a`]: 'c';
};
type Invalid<T extends string> = {
[key: 'a' | 'b' | 'c']: string;
[key: T | number]: string;
[key: Error]: string;
[key: T & string]: string;
};
type Tag1 = {
__tag1__: void;
};
type Tag2 = {
__tag2__: void;
};
type TaggedString1 = string & Tag1;
type TaggedString2 = string & Tag2;
declare let s0: string;
declare let s1: TaggedString1;
declare let s2: TaggedString2;
declare let s3: TaggedString1 | TaggedString2;
declare let s4: TaggedString1 & TaggedString2;
interface I1 {
[key: TaggedString1]: string;
}
interface I2 {
[key: TaggedString2]: string;
}
interface I3 {
[key: TaggedString1 | TaggedString2]: string;
}
interface I4 {
[key: TaggedString1 & TaggedString2]: string;
}
declare let i1: I1;
declare let i2: I2;
declare let i3: I3;
declare let i4: I4;
declare let o1: {
[key: TaggedString1]: string;
};
declare let o2: {
[key: TaggedString2]: string;
};
declare let o3: {
[key: TaggedString1 | TaggedString2]: string;
};
declare let o4: {
[key: TaggedString1 & TaggedString2]: string;
};
declare const obj10: {
[x: string]: 0 | 1;
x: 0;
};
declare const obj11: {
[x: number]: 2 | 3;
1: 2;
};
declare const obj12: {
[x: symbol]: 4 | 5;
[sym]: 4;
};
declare const obj13: {
[x: string]: 0 | 2 | 1 | 3;
[x: number]: 2 | 3;
[x: symbol]: 4 | 5;
x: 0;
1: 2;
[sym]: 4;
};
declare const system: unique symbol;
declare const SomeSytePlugin: unique symbol;
interface Plugs {
[key: symbol]: (...args: any) => unknown;
}
declare const plugins: {
user: Plugs;
[system]: Plugs;
};
declare var theAnswer: symbol;
declare var obj: Record<symbol, number>;
declare const directive: unique symbol;
declare function foo<TArg, TRet, TDir>(options: {
[x in string]: (arg: TArg) => TRet;
} & {
[directive]?: TDir;
}): void;
declare let case1: void;
declare let case2: void;
declare let case3: void;
type Pseudo = `&:${string}`;
declare const AmIPseudo1: Pseudo;
declare const AmIPseudo: Pseudo;
type PseudoDeclaration = {
[key in Pseudo]: string;
};
declare const test: PseudoDeclaration;
type FieldPattern = `/${string}`;
declare const path1: FieldPattern;
declare const path2: FieldPattern;
type PathsObject = {
[P in FieldPattern]: object;
};
declare const pathObject: PathsObject;
type IdType = `${number}-${number}-${number}-${number}`;
declare const id: IdType;
type A = Record<IdType, string>;
declare const a: A;
declare let aid: string;
interface AA {
a?: string;
b?: number;
[key: symbol]: string;
}
declare const aa: AA;
declare const obj1: {
[key: symbol]: string;
};
declare const obj2: {
[key: string]: string;
};
declare const obj3: {
[key: number]: string;
};
type Id = string & {
__tag: 'id ';
};
type Rec1 = {
[key: Id]: number;
};
type Rec2 = Record<Id, number>;
type K1 = keyof Rec1;
type K2 = keyof Rec2;
|