mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Add static index signature (#37797)
* Add static index * fix lint * make lint happy * adjust test cases * add more cases * fix changes * Add more case * accept baseline * fix error if extends others * Update vfsUtil.ts * use equal to empty array * static signature of interface is an error * Accept baseline * Check index constraints for static signature * Accpet baseline * Fix crash * fix crash * Accept baseline * Fix regression * Fix crash * always return new array
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
class C {
|
||||
static [s: string]: number;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class C {
|
||||
static [s: string]: number;
|
||||
static [s: number]: 42
|
||||
}
|
||||
|
||||
C["foo"] = 1
|
||||
C.bar = 2;
|
||||
const foo = C["foo"]
|
||||
C[42] = 42
|
||||
C[2] = 2;
|
||||
const bar = C[42]
|
||||
@@ -0,0 +1,11 @@
|
||||
class C {
|
||||
static readonly [s: string]: number;
|
||||
static readonly [s: number]: 42
|
||||
}
|
||||
|
||||
C["foo"] = 1
|
||||
C.bar = 2;
|
||||
const foo = C["foo"]
|
||||
C[42] = 42
|
||||
C[2] = 2;
|
||||
const bar = C[42]
|
||||
@@ -0,0 +1,28 @@
|
||||
// @strict: true
|
||||
|
||||
class B {
|
||||
static readonly [s: string]: number;
|
||||
static readonly [s: number]: 42 | 233
|
||||
}
|
||||
|
||||
class D extends B {
|
||||
static readonly [s: string]: number
|
||||
}
|
||||
|
||||
class ED extends D {
|
||||
static readonly [s: string]: boolean
|
||||
static readonly [s: number]: 1
|
||||
}
|
||||
|
||||
class DD extends D {
|
||||
static readonly [s: string]: 421
|
||||
}
|
||||
|
||||
const a = B["f"];
|
||||
const b = B[42];
|
||||
const c = D["f"]
|
||||
const d = D[42]
|
||||
const e = ED["f"]
|
||||
const f = ED[42]
|
||||
const g = DD["f"]
|
||||
const h = DD[42]
|
||||
@@ -0,0 +1,37 @@
|
||||
// @strict: true
|
||||
|
||||
class B {
|
||||
static readonly [s: string]: number;
|
||||
static readonly [s: number]: 42 | 233
|
||||
}
|
||||
|
||||
class D {
|
||||
static [s: string]: number;
|
||||
static [s: number]: 42 | 233
|
||||
}
|
||||
|
||||
interface IB {
|
||||
static [s: string]: number;
|
||||
static [s: number]: 42 | 233;
|
||||
}
|
||||
|
||||
declare const v: number
|
||||
declare const i: IB
|
||||
if (v === 0) {
|
||||
B.a = D.a
|
||||
B[2] = D[2]
|
||||
} else if (v === 1) {
|
||||
D.a = B.a
|
||||
D[2] = B[2]
|
||||
} else if (v === 2) {
|
||||
B.a = i.a
|
||||
B[2] = i[2]
|
||||
D.a = i.a
|
||||
D[2] = i [2]
|
||||
} else if (v === 3) {
|
||||
i.a = B.a
|
||||
i[2] = B[2]
|
||||
} else if (v === 4) {
|
||||
i.a = D.a
|
||||
i[2] = B[2]
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// @strict: true
|
||||
|
||||
class B {
|
||||
static readonly [s: string]: number;
|
||||
static readonly [s: number]: 42 | 233
|
||||
}
|
||||
|
||||
interface I {
|
||||
static readonly [s: string]: number;
|
||||
static readonly [s: number]: 42 | 233
|
||||
}
|
||||
|
||||
type TA = (typeof B)["foo"]
|
||||
type TB = (typeof B)[42]
|
||||
|
||||
type TC = (typeof B)[string]
|
||||
type TD = (typeof B)[number]
|
||||
|
||||
type TE = keyof typeof B;
|
||||
|
||||
type TF = Pick<typeof B, number>
|
||||
type TFI = Pick<I, number>
|
||||
type TG = Omit<typeof B, number>
|
||||
type TGI = Omit<I, number>
|
||||
@@ -0,0 +1,19 @@
|
||||
// @strict: true
|
||||
|
||||
function foo () {
|
||||
return class<T> {
|
||||
static [s: string]: number
|
||||
static [s: number]: 42
|
||||
|
||||
foo(v: T) { return v }
|
||||
}
|
||||
}
|
||||
|
||||
const C = foo()
|
||||
C.a;
|
||||
C.a = 1;
|
||||
C[2];
|
||||
C[2] = 42;
|
||||
|
||||
const c = new C<number>();
|
||||
c.foo(1);
|
||||
@@ -0,0 +1,9 @@
|
||||
// @strict: true
|
||||
class X {
|
||||
static [index: string]: string;
|
||||
static x = 12; // Should error, incompatible with index signature
|
||||
}
|
||||
class Y {
|
||||
static [index: string]: string;
|
||||
static foo() {} // should error, incompatible with index signature
|
||||
}
|
||||
Reference in New Issue
Block a user