-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fixed element access expression writes for divergent write types #55585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jakebailey
merged 1 commit into
microsoft:main
from
Andarist:fix/element-access-expression-writes-divergent-accessors
Sep 13, 2023
+1,182
−12
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
tests/baselines/reference/divergentAccessorsTypes8.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
divergentAccessorsTypes8.ts(20,1): error TS2322: Type 'CSSStyleDeclaration' is not assignable to type 'string'. | ||
divergentAccessorsTypes8.ts(86,1): error TS2322: Type 'number' is not assignable to type 'string'. | ||
divergentAccessorsTypes8.ts(124,1): error TS2322: Type '42' is not assignable to type '"hello"'. | ||
divergentAccessorsTypes8.ts(128,1): error TS2322: Type '"hello"' is not assignable to type '42'. | ||
divergentAccessorsTypes8.ts(146,1): error TS2322: Type 'number' is not assignable to type 'boolean'. | ||
divergentAccessorsTypes8.ts(148,1): error TS2322: Type 'string' is not assignable to type 'boolean'. | ||
divergentAccessorsTypes8.ts(149,1): error TS2322: Type 'null' is not assignable to type 'boolean'. | ||
|
||
|
||
==== divergentAccessorsTypes8.ts (7 errors) ==== | ||
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; | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2322: Type 'CSSStyleDeclaration' is not assignable to type 'string'. | ||
|
||
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; | ||
~~~~~~~~~~ | ||
!!! error TS2322: Type 'number' is not assignable to type 'string'. | ||
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; | ||
~~~~~~~~~~~ | ||
!!! error TS2322: Type '42' is not assignable to type '"hello"'. | ||
i2['prop1'] = "hello"; | ||
|
||
i2['prop2'] = 42; | ||
i2['prop2'] = "hello"; | ||
~~~~~~~~~~~ | ||
!!! error TS2322: Type '"hello"' is not assignable to type '42'. | ||
|
||
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 | ||
~~~~~~ | ||
!!! error TS2322: Type 'number' is not assignable to type 'boolean'. | ||
s1[k1] = true | ||
s1[k1] = '' | ||
~~~~~~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'boolean'. | ||
s1[k1] = null | ||
~~~~~~ | ||
!!! error TS2322: Type 'null' is not assignable to type 'boolean'. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other utils like this one do not have a guard like this so it looks a little bit off. However,
.parent
is actually nullable - aSourceFile
doesn't have a parent. I could guard this before calling this function but personally, I prefer to have it here.