Skip to content

Recreate old less safe for loop behavior #18262

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
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ namespace ts {
}

function createType(flags: TypeFlags): Type {
const result = new Type(checker, flags);
const result = new Type(checker, (flags & TypeFlags.TypeVariable) ? flags | TypeFlags.ContainsTypeVariable : flags);
typeCount++;
result.id = typeCount;
return result;
Expand Down Expand Up @@ -21029,7 +21029,7 @@ namespace ts {

// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
// in this case error about missing name is already reported - do not report extra one
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.TypeVariable)) {
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.ContainsTypeVariable)) {
error(node.expression, Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter);
}

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3244,6 +3244,8 @@ namespace ts {
/* @internal */
JsxAttributes = 1 << 25, // Jsx attributes type
MarkerType = 1 << 26, // Marker type used for variance probing
/* @internal */
ContainsTypeVariable = 1 << 27, // Flag indicating the type contains type variables

/* @internal */
Nullable = Undefined | Null,
Expand Down Expand Up @@ -3273,7 +3275,7 @@ namespace ts {
/* @internal */
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
/* @internal */
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType | ContainsTypeVariable
}

export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/forInOverUnionKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [forInOverUnionKeys.ts]
type TypeDescription<T> = {[prop in keyof T]: undefined };

function getAllProperties<T>(typeDesc: (T|TypeDescription<T>)): ReadonlyArray<keyof T>
{
const props: Array<keyof T> = [];

for (var iPropName in typeDesc)
{
props.push(iPropName);
}

return props;
}


//// [forInOverUnionKeys.js]
function getAllProperties(typeDesc) {
var props = [];
for (var iPropName in typeDesc) {
props.push(iPropName);
}
return props;
}
37 changes: 37 additions & 0 deletions tests/baselines/reference/forInOverUnionKeys.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/forInOverUnionKeys.ts ===
type TypeDescription<T> = {[prop in keyof T]: undefined };
>TypeDescription : Symbol(TypeDescription, Decl(forInOverUnionKeys.ts, 0, 0))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 0, 21))
>prop : Symbol(prop, Decl(forInOverUnionKeys.ts, 0, 28))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 0, 21))

function getAllProperties<T>(typeDesc: (T|TypeDescription<T>)): ReadonlyArray<keyof T>
>getAllProperties : Symbol(getAllProperties, Decl(forInOverUnionKeys.ts, 0, 58))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 2, 26))
>typeDesc : Symbol(typeDesc, Decl(forInOverUnionKeys.ts, 2, 29))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 2, 26))
>TypeDescription : Symbol(TypeDescription, Decl(forInOverUnionKeys.ts, 0, 0))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 2, 26))
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.d.ts, --, --))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 2, 26))
{
const props: Array<keyof T> = [];
>props : Symbol(props, Decl(forInOverUnionKeys.ts, 4, 9))
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>T : Symbol(T, Decl(forInOverUnionKeys.ts, 2, 26))

for (var iPropName in typeDesc)
>iPropName : Symbol(iPropName, Decl(forInOverUnionKeys.ts, 6, 12))
>typeDesc : Symbol(typeDesc, Decl(forInOverUnionKeys.ts, 2, 29))
{
props.push(iPropName);
>props.push : Symbol(Array.push, Decl(lib.d.ts, --, --))
>props : Symbol(props, Decl(forInOverUnionKeys.ts, 4, 9))
>push : Symbol(Array.push, Decl(lib.d.ts, --, --))
>iPropName : Symbol(iPropName, Decl(forInOverUnionKeys.ts, 6, 12))
}

return props;
>props : Symbol(props, Decl(forInOverUnionKeys.ts, 4, 9))
}

39 changes: 39 additions & 0 deletions tests/baselines/reference/forInOverUnionKeys.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/compiler/forInOverUnionKeys.ts ===
type TypeDescription<T> = {[prop in keyof T]: undefined };
>TypeDescription : TypeDescription<T>
>T : T
>prop : prop
>T : T

function getAllProperties<T>(typeDesc: (T|TypeDescription<T>)): ReadonlyArray<keyof T>
>getAllProperties : <T>(typeDesc: T | TypeDescription<T>) => ReadonlyArray<keyof T>
>T : T
>typeDesc : T | TypeDescription<T>
>T : T
>TypeDescription : TypeDescription<T>
>T : T
>ReadonlyArray : ReadonlyArray<T>
>T : T
{
const props: Array<keyof T> = [];
>props : (keyof T)[]
>Array : T[]
>T : T
>[] : undefined[]

for (var iPropName in typeDesc)
>iPropName : keyof (T | TypeDescription<T>)
>typeDesc : T | TypeDescription<T>
{
props.push(iPropName);
>props.push(iPropName) : number
>props.push : (...items: (keyof T)[]) => number
>props : (keyof T)[]
>push : (...items: (keyof T)[]) => number
>iPropName : keyof (T | TypeDescription<T>)
}

return props;
>props : (keyof T)[]
}

13 changes: 13 additions & 0 deletions tests/cases/compiler/forInOverUnionKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type TypeDescription<T> = {[prop in keyof T]: undefined };

function getAllProperties<T>(typeDesc: (T|TypeDescription<T>)): ReadonlyArray<keyof T>
{
const props: Array<keyof T> = [];

for (var iPropName in typeDesc)
{
props.push(iPropName);
}

return props;
}