Skip to content

use string as type of variable in ForInStatement #6302

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2510,9 +2510,9 @@ namespace ts {

// Return the inferred type for a variable, parameter, or property declaration
function getTypeForVariableLikeDeclaration(declaration: VariableLikeDeclaration): Type {
// A variable declared in a for..in statement is always of type any
// A variable declared in a for..in statement is always of type string
if (declaration.parent.parent.kind === SyntaxKind.ForInStatement) {
return anyType;
return stringType;
}

if (declaration.parent.parent.kind === SyntaxKind.ForOfStatement) {
Expand Down Expand Up @@ -12691,8 +12691,11 @@ namespace ts {
}
}

const isVariableInForInStatement = node.parent.parent.kind === SyntaxKind.ForInStatement;
// For a binding pattern, check contained binding elements
if (isBindingPattern(node.name)) {
// NOTE: do not check them if this is a binding pattern in variable declaration of ForIn statement
// this situation is already illegal so no need to report misleading errors.
if (isBindingPattern(node.name) && !isVariableInForInStatement) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not allowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we have not implemented this yet: #3715. I think I'll remove this check so it won't be missed once #3715 is addresssed

forEach((<BindingPattern>node.name).elements, checkSourceElement);
}
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
Expand All @@ -12702,7 +12705,9 @@ namespace ts {
}
// For a binding pattern, validate the initializer and exit
if (isBindingPattern(node.name)) {
if (node.initializer) {
// do not check initializer if this is variable in ForIn statement.
// this situation is already illegal so no need to report misleading errors.
if (node.initializer && !isVariableInForInStatement) {
checkTypeAssignableTo(checkExpressionCached(node.initializer), getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined);
checkParameterInitializer(node);
}
Expand All @@ -12712,7 +12717,9 @@ namespace ts {
const type = getTypeOfVariableOrParameterOrProperty(symbol);
if (node === symbol.valueDeclaration) {
// Node is the primary declaration of the symbol, just validate the initializer
if (node.initializer) {
// NOTE: do not check initializer if this is variable in ForIn statement.
// this situation is already illegal so no need to report misleading errors.
if (node.initializer && !isVariableInForInStatement) {
checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined);
checkParameterInitializer(node);
}
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/capturedLetConstInLoop1.types
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
=== tests/cases/compiler/capturedLetConstInLoop1.ts ===
//==== let
for (let x in {}) {
>x : any
>x : string
>{} : {}

(function() { return x});
>(function() { return x}) : () => any
>function() { return x} : () => any
>x : any
>(function() { return x}) : () => string
>function() { return x} : () => string
>x : string

(() => x);
>(() => x) : () => any
>() => x : () => any
>x : any
>(() => x) : () => string
>() => x : () => string
>x : string
}

for (let x of []) {
Expand Down Expand Up @@ -216,18 +216,18 @@ for (let y = 0; y < 1; ++y) {

//=========const
for (const x in {}) {
>x : any
>x : string
>{} : {}

(function() { return x});
>(function() { return x}) : () => any
>function() { return x} : () => any
>x : any
>(function() { return x}) : () => string
>function() { return x} : () => string
>x : string

(() => x);
>(() => x) : () => any
>() => x : () => any
>x : any
>(() => x) : () => string
>() => x : () => string
>x : string
}

for (const x of []) {
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/capturedLetConstInLoop1_ES6.types
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
=== tests/cases/compiler/capturedLetConstInLoop1_ES6.ts ===
//==== let
for (let x in {}) {
>x : any
>x : string
>{} : {}

(function() { return x});
>(function() { return x}) : () => any
>function() { return x} : () => any
>x : any
>(function() { return x}) : () => string
>function() { return x} : () => string
>x : string

(() => x);
>(() => x) : () => any
>() => x : () => any
>x : any
>(() => x) : () => string
>() => x : () => string
>x : string
}

for (let x of []) {
Expand Down Expand Up @@ -216,18 +216,18 @@ for (let y = 0; y < 1; ++y) {

//=========const
for (const x in {}) {
>x : any
>x : string
>{} : {}

(function() { return x});
>(function() { return x}) : () => any
>function() { return x} : () => any
>x : any
>(function() { return x}) : () => string
>function() { return x} : () => string
>x : string

(() => x);
>(() => x) : () => any
>() => x : () => any
>x : any
>(() => x) : () => string
>() => x : () => string
>x : string
}

for (const x of []) {
Expand Down
36 changes: 18 additions & 18 deletions tests/baselines/reference/capturedLetConstInLoop2.types
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function foo0_1(x) {
>x : any

for (let x in []) {
>x : any
>x : string
>[] : undefined[]

let a = arguments.length;
Expand All @@ -47,17 +47,17 @@ function foo0_1(x) {
>length : number

(function() { return x + a });
>(function() { return x + a }) : () => any
>function() { return x + a } : () => any
>x + a : any
>x : any
>(function() { return x + a }) : () => string
>function() { return x + a } : () => string
>x + a : string
>x : string
>a : number

(() => x + a);
>(() => x + a) : () => any
>() => x + a : () => any
>x + a : any
>x : any
>(() => x + a) : () => string
>() => x + a : () => string
>x + a : string
>x : string
>a : number
}
}
Expand Down Expand Up @@ -400,7 +400,7 @@ function foo0_1_c(x) {
>x : any

for (const x in []) {
>x : any
>x : string
>[] : undefined[]

const a = arguments.length;
Expand All @@ -410,17 +410,17 @@ function foo0_1_c(x) {
>length : number

(function() { return x + a });
>(function() { return x + a }) : () => any
>function() { return x + a } : () => any
>x + a : any
>x : any
>(function() { return x + a }) : () => string
>function() { return x + a } : () => string
>x + a : string
>x : string
>a : number

(() => x + a);
>(() => x + a) : () => any
>() => x + a : () => any
>x + a : any
>x : any
>(() => x + a) : () => string
>() => x + a : () => string
>x + a : string
>x : string
>a : number
}
}
Expand Down
36 changes: 18 additions & 18 deletions tests/baselines/reference/capturedLetConstInLoop2_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function foo0_1(x) {
>x : any

for (let x in []) {
>x : any
>x : string
>[] : undefined[]

let a = arguments.length;
Expand All @@ -46,17 +46,17 @@ function foo0_1(x) {
>length : number

(function() { return x + a });
>(function() { return x + a }) : () => any
>function() { return x + a } : () => any
>x + a : any
>x : any
>(function() { return x + a }) : () => string
>function() { return x + a } : () => string
>x + a : string
>x : string
>a : number

(() => x + a);
>(() => x + a) : () => any
>() => x + a : () => any
>x + a : any
>x : any
>(() => x + a) : () => string
>() => x + a : () => string
>x + a : string
>x : string
>a : number
}
}
Expand Down Expand Up @@ -399,7 +399,7 @@ function foo0_1_c(x) {
>x : any

for (const x in []) {
>x : any
>x : string
>[] : undefined[]

const a = arguments.length;
Expand All @@ -409,17 +409,17 @@ function foo0_1_c(x) {
>length : number

(function() { return x + a });
>(function() { return x + a }) : () => any
>function() { return x + a } : () => any
>x + a : any
>x : any
>(function() { return x + a }) : () => string
>function() { return x + a } : () => string
>x + a : string
>x : string
>a : number

(() => x + a);
>(() => x + a) : () => any
>() => x + a : () => any
>x + a : any
>x : any
>(() => x + a) : () => string
>() => x + a : () => string
>x + a : string
>x : string
>a : number
}
}
Expand Down
56 changes: 28 additions & 28 deletions tests/baselines/reference/capturedLetConstInLoop3.types
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,32 @@ function foo0_1(x) {
>x : any

for (let x in []) {
>x : any
>x : string
>[] : undefined[]

var v = x;
>v : any
>x : any
>v : string
>x : string

(function() { return x + v });
>(function() { return x + v }) : () => any
>function() { return x + v } : () => any
>x + v : any
>x : any
>v : any
>(function() { return x + v }) : () => string
>function() { return x + v } : () => string
>x + v : string
>x : string
>v : string

(() => x + v);
>(() => x + v) : () => any
>() => x + v : () => any
>x + v : any
>x : any
>v : any
>(() => x + v) : () => string
>() => x + v : () => string
>x + v : string
>x : string
>v : string
}

use(v);
>use(v) : any
>use : (a: any) => any
>v : any
>v : string
}

function foo1(x) {
Expand Down Expand Up @@ -438,32 +438,32 @@ function foo0_1_c(x) {
>x : any

for (const x in []) {
>x : any
>x : string
>[] : undefined[]

var v = x;
>v : any
>x : any
>v : string
>x : string

(function() { return x + v });
>(function() { return x + v }) : () => any
>function() { return x + v } : () => any
>x + v : any
>x : any
>v : any
>(function() { return x + v }) : () => string
>function() { return x + v } : () => string
>x + v : string
>x : string
>v : string

(() => x + v);
>(() => x + v) : () => any
>() => x + v : () => any
>x + v : any
>x : any
>v : any
>(() => x + v) : () => string
>() => x + v : () => string
>x + v : string
>x : string
>v : string
}

use(v);
>use(v) : any
>use : (a: any) => any
>v : any
>v : string
}

function foo1_c(x) {
Expand Down
Loading