-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Consistent widening in access expressions #31802
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1355729
Consistent widening in property and element access expressions
ahejlsberg ddc742c
Minor fix
ahejlsberg 9d2a2c9
Add tests
ahejlsberg 1aed26a
Accept new baselines
ahejlsberg 69706bd
Merge branch 'master' into wideningInAccessExpressions
ahejlsberg 4856768
Merge branch 'master' into wideningInAccessExpressions
ahejlsberg 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
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/propertyAccessWidening.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,34 @@ | ||
tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts(18,21): error TS2339: Property 'a' does not exist on type '{ a: string; b: number; } | {}'. | ||
Property 'a' does not exist on type '{}'. | ||
tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts(19,5): error TS7053: Element implicitly has an 'any' type because expression of type '"a"' can't be used to index type '{}'. | ||
Property 'a' does not exist on type '{}'. | ||
|
||
|
||
==== tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts (2 errors) ==== | ||
// Repro from #31762 | ||
|
||
function g1(headerNames: any) { | ||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
const table = [{cells: headerNames }].concat(t); | ||
} | ||
|
||
function g2(headerNames: any) { | ||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
const table = [{cells: headerNames }]["concat"](t); | ||
} | ||
|
||
// Object in property or element access is widened when target of assignment | ||
|
||
function foo(options?: { a: string, b: number }) { | ||
let x1 = (options || {}).a; // Object type not widened | ||
let x2 = (options || {})["a"]; // Object type not widened | ||
(options || {}).a = 1; // Object type widened, error | ||
~ | ||
!!! error TS2339: Property 'a' does not exist on type '{ a: string; b: number; } | {}'. | ||
!!! error TS2339: Property 'a' does not exist on type '{}'. | ||
(options || {})["a"] = 1; // Object type widened, error | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS7053: Element implicitly has an 'any' type because expression of type '"a"' can't be used to index type '{}'. | ||
!!! error TS7053: Property 'a' does not exist on type '{}'. | ||
} | ||
|
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,41 @@ | ||
//// [propertyAccessWidening.ts] | ||
// Repro from #31762 | ||
|
||
function g1(headerNames: any) { | ||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
const table = [{cells: headerNames }].concat(t); | ||
} | ||
|
||
function g2(headerNames: any) { | ||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
const table = [{cells: headerNames }]["concat"](t); | ||
} | ||
|
||
// Object in property or element access is widened when target of assignment | ||
|
||
function foo(options?: { a: string, b: number }) { | ||
let x1 = (options || {}).a; // Object type not widened | ||
let x2 = (options || {})["a"]; // Object type not widened | ||
(options || {}).a = 1; // Object type widened, error | ||
(options || {})["a"] = 1; // Object type widened, error | ||
} | ||
|
||
|
||
//// [propertyAccessWidening.js] | ||
"use strict"; | ||
// Repro from #31762 | ||
function g1(headerNames) { | ||
var t = [{ hasLineBreak: false, cells: [] }]; | ||
var table = [{ cells: headerNames }].concat(t); | ||
} | ||
function g2(headerNames) { | ||
var t = [{ hasLineBreak: false, cells: [] }]; | ||
var table = [{ cells: headerNames }]["concat"](t); | ||
} | ||
// Object in property or element access is widened when target of assignment | ||
function foo(options) { | ||
var x1 = (options || {}).a; // Object type not widened | ||
var x2 = (options || {})["a"]; // Object type not widened | ||
(options || {}).a = 1; // Object type widened, error | ||
(options || {})["a"] = 1; // Object type widened, error | ||
} |
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,65 @@ | ||
=== tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts === | ||
// Repro from #31762 | ||
|
||
function g1(headerNames: any) { | ||
>g1 : Symbol(g1, Decl(propertyAccessWidening.ts, 0, 0)) | ||
>headerNames : Symbol(headerNames, Decl(propertyAccessWidening.ts, 2, 12)) | ||
|
||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
>t : Symbol(t, Decl(propertyAccessWidening.ts, 3, 7)) | ||
>hasLineBreak : Symbol(hasLineBreak, Decl(propertyAccessWidening.ts, 3, 14)) | ||
>cells : Symbol(cells, Decl(propertyAccessWidening.ts, 3, 35)) | ||
|
||
const table = [{cells: headerNames }].concat(t); | ||
>table : Symbol(table, Decl(propertyAccessWidening.ts, 4, 9)) | ||
>[{cells: headerNames }].concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cells : Symbol(cells, Decl(propertyAccessWidening.ts, 4, 20)) | ||
>headerNames : Symbol(headerNames, Decl(propertyAccessWidening.ts, 2, 12)) | ||
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>t : Symbol(t, Decl(propertyAccessWidening.ts, 3, 7)) | ||
} | ||
|
||
function g2(headerNames: any) { | ||
>g2 : Symbol(g2, Decl(propertyAccessWidening.ts, 5, 1)) | ||
>headerNames : Symbol(headerNames, Decl(propertyAccessWidening.ts, 7, 12)) | ||
|
||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
>t : Symbol(t, Decl(propertyAccessWidening.ts, 8, 7)) | ||
>hasLineBreak : Symbol(hasLineBreak, Decl(propertyAccessWidening.ts, 8, 14)) | ||
>cells : Symbol(cells, Decl(propertyAccessWidening.ts, 8, 35)) | ||
|
||
const table = [{cells: headerNames }]["concat"](t); | ||
>table : Symbol(table, Decl(propertyAccessWidening.ts, 9, 9)) | ||
>cells : Symbol(cells, Decl(propertyAccessWidening.ts, 9, 20)) | ||
>headerNames : Symbol(headerNames, Decl(propertyAccessWidening.ts, 7, 12)) | ||
>"concat" : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>t : Symbol(t, Decl(propertyAccessWidening.ts, 8, 7)) | ||
} | ||
|
||
// Object in property or element access is widened when target of assignment | ||
|
||
function foo(options?: { a: string, b: number }) { | ||
>foo : Symbol(foo, Decl(propertyAccessWidening.ts, 10, 1)) | ||
>options : Symbol(options, Decl(propertyAccessWidening.ts, 14, 13)) | ||
>a : Symbol(a, Decl(propertyAccessWidening.ts, 14, 24)) | ||
>b : Symbol(b, Decl(propertyAccessWidening.ts, 14, 35)) | ||
|
||
let x1 = (options || {}).a; // Object type not widened | ||
>x1 : Symbol(x1, Decl(propertyAccessWidening.ts, 15, 7)) | ||
>(options || {}).a : Symbol(a, Decl(propertyAccessWidening.ts, 14, 24)) | ||
>options : Symbol(options, Decl(propertyAccessWidening.ts, 14, 13)) | ||
>a : Symbol(a, Decl(propertyAccessWidening.ts, 14, 24)) | ||
|
||
let x2 = (options || {})["a"]; // Object type not widened | ||
>x2 : Symbol(x2, Decl(propertyAccessWidening.ts, 16, 7)) | ||
>options : Symbol(options, Decl(propertyAccessWidening.ts, 14, 13)) | ||
>"a" : Symbol(a, Decl(propertyAccessWidening.ts, 14, 24)) | ||
|
||
(options || {}).a = 1; // Object type widened, error | ||
>options : Symbol(options, Decl(propertyAccessWidening.ts, 14, 13)) | ||
|
||
(options || {})["a"] = 1; // Object type widened, error | ||
>options : Symbol(options, Decl(propertyAccessWidening.ts, 14, 13)) | ||
>"a" : Symbol(a, Decl(propertyAccessWidening.ts, 14, 24)) | ||
} | ||
|
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,100 @@ | ||
=== tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts === | ||
// Repro from #31762 | ||
|
||
function g1(headerNames: any) { | ||
>g1 : (headerNames: any) => void | ||
>headerNames : any | ||
|
||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
>t : { hasLineBreak: boolean; cells: never[]; }[] | ||
>[{ hasLineBreak: false, cells: [] }] : { hasLineBreak: boolean; cells: never[]; }[] | ||
>{ hasLineBreak: false, cells: [] } : { hasLineBreak: boolean; cells: never[]; } | ||
>hasLineBreak : boolean | ||
>false : false | ||
>cells : never[] | ||
>[] : never[] | ||
|
||
const table = [{cells: headerNames }].concat(t); | ||
>table : { cells: any; }[] | ||
>[{cells: headerNames }].concat(t) : { cells: any; }[] | ||
>[{cells: headerNames }].concat : { (...items: ConcatArray<{ cells: any; }>[]): { cells: any; }[]; (...items: ({ cells: any; } | ConcatArray<{ cells: any; }>)[]): { cells: any; }[]; } | ||
>[{cells: headerNames }] : { cells: any; }[] | ||
>{cells: headerNames } : { cells: any; } | ||
>cells : any | ||
>headerNames : any | ||
>concat : { (...items: ConcatArray<{ cells: any; }>[]): { cells: any; }[]; (...items: ({ cells: any; } | ConcatArray<{ cells: any; }>)[]): { cells: any; }[]; } | ||
>t : { hasLineBreak: boolean; cells: never[]; }[] | ||
} | ||
|
||
function g2(headerNames: any) { | ||
>g2 : (headerNames: any) => void | ||
>headerNames : any | ||
|
||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
>t : { hasLineBreak: boolean; cells: never[]; }[] | ||
>[{ hasLineBreak: false, cells: [] }] : { hasLineBreak: boolean; cells: never[]; }[] | ||
>{ hasLineBreak: false, cells: [] } : { hasLineBreak: boolean; cells: never[]; } | ||
>hasLineBreak : boolean | ||
>false : false | ||
>cells : never[] | ||
>[] : never[] | ||
|
||
const table = [{cells: headerNames }]["concat"](t); | ||
>table : { cells: any; }[] | ||
>[{cells: headerNames }]["concat"](t) : { cells: any; }[] | ||
>[{cells: headerNames }]["concat"] : { (...items: ConcatArray<{ cells: any; }>[]): { cells: any; }[]; (...items: ({ cells: any; } | ConcatArray<{ cells: any; }>)[]): { cells: any; }[]; } | ||
>[{cells: headerNames }] : { cells: any; }[] | ||
>{cells: headerNames } : { cells: any; } | ||
>cells : any | ||
>headerNames : any | ||
>"concat" : "concat" | ||
>t : { hasLineBreak: boolean; cells: never[]; }[] | ||
} | ||
|
||
// Object in property or element access is widened when target of assignment | ||
|
||
function foo(options?: { a: string, b: number }) { | ||
>foo : (options?: { a: string; b: number; } | undefined) => void | ||
>options : { a: string; b: number; } | undefined | ||
>a : string | ||
>b : number | ||
|
||
let x1 = (options || {}).a; // Object type not widened | ||
>x1 : string | undefined | ||
>(options || {}).a : string | undefined | ||
>(options || {}) : { a: string; b: number; } | {} | ||
>options || {} : { a: string; b: number; } | {} | ||
>options : { a: string; b: number; } | undefined | ||
>{} : {} | ||
>a : string | undefined | ||
|
||
let x2 = (options || {})["a"]; // Object type not widened | ||
>x2 : string | undefined | ||
>(options || {})["a"] : string | undefined | ||
>(options || {}) : { a: string; b: number; } | {} | ||
>options || {} : { a: string; b: number; } | {} | ||
>options : { a: string; b: number; } | undefined | ||
>{} : {} | ||
>"a" : "a" | ||
|
||
(options || {}).a = 1; // Object type widened, error | ||
>(options || {}).a = 1 : 1 | ||
>(options || {}).a : any | ||
>(options || {}) : { a: string; b: number; } | {} | ||
>options || {} : { a: string; b: number; } | {} | ||
>options : { a: string; b: number; } | undefined | ||
>{} : {} | ||
>a : any | ||
>1 : 1 | ||
|
||
(options || {})["a"] = 1; // Object type widened, error | ||
>(options || {})["a"] = 1 : 1 | ||
>(options || {})["a"] : any | ||
>(options || {}) : { a: string; b: number; } | {} | ||
>options || {} : { a: string; b: number; } | {} | ||
>options : { a: string; b: number; } | undefined | ||
>{} : {} | ||
>"a" : "a" | ||
>1 : 1 | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
tests/cases/conformance/expressions/propertyAccess/propertyAccessWidening.ts
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,22 @@ | ||
// @strict: true | ||
|
||
// Repro from #31762 | ||
|
||
function g1(headerNames: any) { | ||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
const table = [{cells: headerNames }].concat(t); | ||
} | ||
|
||
function g2(headerNames: any) { | ||
let t = [{ hasLineBreak: false, cells: [] }]; | ||
const table = [{cells: headerNames }]["concat"](t); | ||
} | ||
|
||
// Object in property or element access is widened when target of assignment | ||
|
||
function foo(options?: { a: string, b: number }) { | ||
let x1 = (options || {}).a; // Object type not widened | ||
let x2 = (options || {})["a"]; // Object type not widened | ||
(options || {}).a = 1; // Object type widened, error | ||
(options || {})["a"] = 1; // Object type widened, error | ||
} |
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.
This should probably? also skip both kinds of casts.
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.
No, if there is an intervening type assertion then that specifies the type and widening isn't relevant.