Skip to content
Merged
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
17 changes: 7 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4151,13 +4151,13 @@ namespace ts {
if (parentType === unknownType) {
return unknownType;
}
// If no type was specified or inferred for parent, or if the specified or inferred type is any,
// infer from the initializer of the binding element if one is present. Otherwise, go with the
// undefined or any type of the parent.
if (!parentType || isTypeAny(parentType)) {
if (declaration.initializer) {
return checkDeclarationInitializer(declaration);
}
// If no type was specified or inferred for parent,
// infer from the initializer of the binding element if one is present.
// Otherwise, go with the undefined type of the parent.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Doesn't it make sense to say "Otherwise, go with the any type of the parent"? We don't seem to do any checking for the undefined type here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This applies to the block immediately below the comment, which checks if (!parentType). "The falsy type of the parent" would be more accurate, but isn't the intent.

Any is different in that (1) you can specify any and (2) it will override the initialiser. I decided not to comment this as it seemed pretty obvious.

if (!parentType) {
return declaration.initializer ? checkDeclarationInitializer(declaration) : parentType;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If there's no parentType, and no declaration.initializer, then we return parentType when we know it does not exist - it'd probably be nicer for future references to this code if it explicitly returned undefined and had undefined as part of it's return type signature, if you ever feel like coming back to this.

}
if (isTypeAny(parentType)) {
return parentType;
}

Expand All @@ -4183,9 +4183,6 @@ namespace ts {
// computed properties with non-literal names are treated as 'any'
return anyType;
}
if (declaration.initializer) {
getContextualType(declaration.initializer);
}

// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature,
// or otherwise the type of the string index signature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var [a0, a1]: any = undefined;
>undefined : undefined

var [a2 = false, a3 = 1]: any = undefined;
>a2 : boolean
>a2 : any
>false : false
>a3 : number
>a3 : any
>1 : 1
>undefined : undefined

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var [a0, a1]: any = undefined;
>undefined : undefined

var [a2 = false, a3 = 1]: any = undefined;
>a2 : boolean
>a2 : any
>false : false
>a3 : number
>a3 : any
>1 : 1
>undefined : undefined

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var [a0, a1]: any = undefined;
>undefined : undefined

var [a2 = false, a3 = 1]: any = undefined;
>a2 : boolean
>a2 : any
>false : false
>a3 : number
>a3 : any
>1 : 1
>undefined : undefined

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var {1: b3} = { 1: "string" };
>"string" : "string"

var {b4 = 1}: any = { b4: 100000 };
>b4 : number
>b4 : any
>1 : 1
>{ b4: 100000 } : { b4: number; }
>b4 : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var {1: b3} = { 1: "string" };
>"string" : "string"

var {b4 = 1}: any = { b4: 100000 };
>b4 : number
>b4 : any
>1 : 1
>{ b4: 100000 } : { b4: number; }
>b4 : number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
=== tests/cases/conformance/es6/destructuring/destructuringObjectBindingPatternAndAssignment4.ts ===
const {
a = 1,
>a : 1
>a : any
>1 : 1

b = 2,
>b : 2
>b : any
>2 : 2

c = b, // ok
>c : 2
>b : 2
>c : any
>b : any

d = a, // ok
>d : 1
>a : 1
>d : any
>a : any

e = f, // error
>e : any
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRestParameter.types
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class C {
}
function foobar({ bar={}, ...opts }: any = {}) {
>foobar : ({ bar, ...opts }?: any) => void
>bar : {}
>bar : any
>{} : {}
>opts : any
>{} : {}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRestParameterES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class C {
}
function foobar({ bar={}, ...opts }: any = {}) {
>foobar : ({ bar, ...opts }?: any) => void
>bar : {}
>bar : any
>{} : {}
>opts : any
>{} : {}
Expand Down