mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Fixed element access expression writes for divergent write types (#55585)
This commit is contained in:
committed by
GitHub
parent
c0b39c6967
commit
e6321d77c7
@@ -6,12 +6,12 @@ class Test<S> {
|
||||
get value(): string {
|
||||
return null!;
|
||||
}
|
||||
|
||||
|
||||
// -- Replacing the getter such that the getter/setter types match, removes the error:
|
||||
// get value(): string | ((item: S) => string) {
|
||||
// return null!;
|
||||
// }
|
||||
|
||||
|
||||
// -- Or, replacing the setter such that a concrete type is used, removes the error:
|
||||
// set value(value: string | ((item: { property: string }) => string)) {}
|
||||
}
|
||||
@@ -21,3 +21,4 @@ const a = new Test<{
|
||||
}>();
|
||||
|
||||
a.value = (item) => item.property
|
||||
a['value'] = (item) => item.property
|
||||
|
||||
153
tests/cases/compiler/divergentAccessorsTypes8.ts
Normal file
153
tests/cases/compiler/divergentAccessorsTypes8.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
// @strict: true
|
||||
// @lib: esnext, dom
|
||||
// @noEmit: true
|
||||
|
||||
export {}
|
||||
|
||||
interface Serializer {
|
||||
set value(v: string | number | boolean);
|
||||
get value(): string;
|
||||
}
|
||||
declare let box: Serializer;
|
||||
const v = box['value']
|
||||
box['value'] = true;
|
||||
box['value'] = 42;
|
||||
box['value'] = "hello";
|
||||
|
||||
interface Element {
|
||||
get style(): CSSStyleDeclaration;
|
||||
set style(cssText: string);
|
||||
}
|
||||
|
||||
declare const element: Element;
|
||||
element['style'] = "color: red";
|
||||
element['style'] = element.style;
|
||||
|
||||
class One {
|
||||
get prop1(): string {
|
||||
return "";
|
||||
}
|
||||
set prop1(s: string | number) {}
|
||||
|
||||
get prop2(): string {
|
||||
return "";
|
||||
}
|
||||
set prop2(s: string | number) {}
|
||||
|
||||
prop3: number = 42;
|
||||
|
||||
get prop4(): string {
|
||||
return "";
|
||||
}
|
||||
set prop4(s: string | number) {}
|
||||
}
|
||||
|
||||
class Two {
|
||||
get prop1(): string {
|
||||
return "";
|
||||
}
|
||||
set prop1(s: string | number) {}
|
||||
|
||||
get prop2(): string {
|
||||
return "";
|
||||
}
|
||||
set prop2(s: string) {}
|
||||
|
||||
get prop3(): string {
|
||||
return "";
|
||||
}
|
||||
set prop3(s: string | boolean) {}
|
||||
|
||||
get prop4(): string {
|
||||
return "";
|
||||
}
|
||||
set prop4(s: string | boolean) {}
|
||||
}
|
||||
|
||||
declare const u1: One | Two;
|
||||
|
||||
u1['prop1'] = 42;
|
||||
u1['prop1'] = "hello";
|
||||
|
||||
u1['prop2'] = 42;
|
||||
u1['prop2'] = "hello";
|
||||
|
||||
u1['prop3'] = 42;
|
||||
u1['prop3'] = "hello";
|
||||
u1['prop3'] = true;
|
||||
|
||||
u1['prop4'] = 42;
|
||||
u1['prop4'] = "hello";
|
||||
u1['prop4'] = true;
|
||||
|
||||
declare const i: One & Two;
|
||||
|
||||
const iv1 = i['prop1'];
|
||||
i['prop1'] = 42;
|
||||
i['prop1'] = "hello";
|
||||
|
||||
const iv2 = i['prop2'];
|
||||
i['prop2'] = 42;
|
||||
i['prop2'] = "hello";
|
||||
|
||||
class Three {
|
||||
get prop1(): string {
|
||||
return "";
|
||||
}
|
||||
set prop1(s: string | number) {}
|
||||
|
||||
prop2: number = 42;
|
||||
}
|
||||
|
||||
class Four {
|
||||
get prop1(): "hello" {
|
||||
return "hello";
|
||||
}
|
||||
set prop1(s: "hello" | number) {}
|
||||
|
||||
get prop2(): string {
|
||||
return "";
|
||||
}
|
||||
set prop2(s: string | 42) {}
|
||||
}
|
||||
|
||||
class Five {
|
||||
get prop1(): "hello" {
|
||||
return "hello";
|
||||
}
|
||||
set prop1(s: "hello" | boolean) {}
|
||||
|
||||
get prop2(): string {
|
||||
return "";
|
||||
}
|
||||
set prop2(s: string | number | boolean) {}
|
||||
}
|
||||
|
||||
declare const i2: Three & Four & Five;
|
||||
|
||||
i2['prop1'] = 42;
|
||||
i2['prop1'] = "hello";
|
||||
|
||||
i2['prop2'] = 42;
|
||||
i2['prop2'] = "hello";
|
||||
|
||||
class Six {
|
||||
get prop1(): boolean | number {
|
||||
return 42;
|
||||
}
|
||||
set prop1(s: boolean | string) {}
|
||||
|
||||
get prop2(): bigint | number {
|
||||
return 10;
|
||||
}
|
||||
set prop2(s: boolean | null) {}
|
||||
}
|
||||
|
||||
declare const s1: Six
|
||||
declare const k1: 'prop1' | 'prop2'
|
||||
|
||||
const sv1 = s1[k1]
|
||||
s1[k1] = 42
|
||||
s1[k1] = true
|
||||
s1[k1] = ''
|
||||
s1[k1] = null
|
||||
@@ -0,0 +1,8 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// @strict: true
|
||||
// @exactOptionalPropertyTypes: true
|
||||
//// declare const xx: { prop?: number };
|
||||
//// xx['prop'/*1*/] = 1;
|
||||
|
||||
verify.quickInfoAt('1', '(property) prop?: number');
|
||||
@@ -0,0 +1,8 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// @strict: true
|
||||
// @exactOptionalPropertyTypes: true
|
||||
//// declare const xx: { prop?: number };
|
||||
//// xx['prop'/*1*/] += 1;
|
||||
|
||||
verify.quickInfoAt('1', '(property) prop?: number');
|
||||
@@ -0,0 +1,8 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// @strict: true
|
||||
// @exactOptionalPropertyTypes: true
|
||||
//// declare const xx: { prop?: number };
|
||||
//// xx['prop'/*1*/] ??= 1;
|
||||
|
||||
verify.quickInfoAt('1', '(property) prop?: number');
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// @strict: true
|
||||
//// interface Serializer {
|
||||
//// set value(v: string | number | boolean);
|
||||
//// get value(): string;
|
||||
//// }
|
||||
//// declare let box: Serializer;
|
||||
//// box['value'/*1*/] = true;
|
||||
|
||||
verify.quickInfoAt('1', '(property) Serializer.value: string | number | boolean');
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// @strict: true
|
||||
//// interface Serializer {
|
||||
//// set value(v: string | number);
|
||||
//// get value(): string;
|
||||
//// }
|
||||
//// declare let box: Serializer;
|
||||
//// box['value'/*1*/] += 10;
|
||||
|
||||
verify.quickInfoAt('1', '(property) Serializer.value: string | number');
|
||||
Reference in New Issue
Block a user