mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 03:09:39 -06:00
Guard destructuring initializer contextual typing
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
parent
43898d202f
commit
2abf5a1a7b
@ -28312,7 +28312,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
// from its initializer, we'll already have cached the type. Otherwise we compute it now
|
||||
// without caching such that transient types are reflected.
|
||||
const links = getNodeLinks(node);
|
||||
return links.resolvedType || getTypeOfExpression(node);
|
||||
if (links.resolvedType) {
|
||||
return links.resolvedType;
|
||||
}
|
||||
const pattern = isVariableDeclaration(node.parent) && node.parent.initializer === node && isBindingPattern(node.parent.name)
|
||||
? node.parent.name
|
||||
: undefined;
|
||||
if (pattern) {
|
||||
contextualBindingPatterns.push(pattern);
|
||||
const type = getTypeOfExpression(node);
|
||||
contextualBindingPatterns.pop();
|
||||
return type;
|
||||
}
|
||||
return getTypeOfExpression(node);
|
||||
}
|
||||
|
||||
function getInitialTypeOfVariableDeclaration(node: VariableDeclaration) {
|
||||
@ -31023,7 +31035,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
// If the identifier is declared in a binding pattern for which we're currently computing the implied type and the
|
||||
// reference occurs with the same binding pattern, return the non-inferrable any type. This for example occurs in
|
||||
// 'const [a, b = a + 1] = [2]' when we're computing the contextual type for the array literal '[2]'.
|
||||
if (declaration && declaration.kind === SyntaxKind.BindingElement && contains(contextualBindingPatterns, declaration.parent) && findAncestor(node, parent => parent === declaration!.parent)) {
|
||||
if (declaration && declaration.kind === SyntaxKind.BindingElement && contains(contextualBindingPatterns, declaration.parent)) {
|
||||
return nonInferrableAnyType;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
destructuringContextualBindingStackOverflow.ts(1,7): error TS2322: Type '{ c: number; f: any; }' is not assignable to type 'string | number | symbol'.
|
||||
destructuringContextualBindingStackOverflow.ts(1,9): error TS2339: Property 'c' does not exist on type 'string | number | symbol'.
|
||||
destructuringContextualBindingStackOverflow.ts(1,12): error TS2339: Property 'f' does not exist on type 'string | number | symbol'.
|
||||
destructuringContextualBindingStackOverflow.ts(1,52): error TS2448: Block-scoped variable 'f' used before its declaration.
|
||||
|
||||
|
||||
==== destructuringContextualBindingStackOverflow.ts (4 errors) ====
|
||||
const { c, f }: string | number | symbol = { c: 0, f };
|
||||
~~~~~~~~
|
||||
!!! error TS2322: Type '{ c: number; f: any; }' is not assignable to type 'string | number | symbol'.
|
||||
~
|
||||
!!! error TS2339: Property 'c' does not exist on type 'string | number | symbol'.
|
||||
~
|
||||
!!! error TS2339: Property 'f' does not exist on type 'string | number | symbol'.
|
||||
~
|
||||
!!! error TS2448: Block-scoped variable 'f' used before its declaration.
|
||||
!!! related TS2728 destructuringContextualBindingStackOverflow.ts:1:12: 'f' is declared here.
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
//// [tests/cases/compiler/destructuringContextualBindingStackOverflow.ts] ////
|
||||
|
||||
//// [destructuringContextualBindingStackOverflow.ts]
|
||||
const { c, f }: string | number | symbol = { c: 0, f };
|
||||
|
||||
|
||||
//// [destructuringContextualBindingStackOverflow.js]
|
||||
"use strict";
|
||||
var _a = { c: 0, f: f }, c = _a.c, f = _a.f;
|
||||
@ -0,0 +1,9 @@
|
||||
//// [tests/cases/compiler/destructuringContextualBindingStackOverflow.ts] ////
|
||||
|
||||
=== destructuringContextualBindingStackOverflow.ts ===
|
||||
const { c, f }: string | number | symbol = { c: 0, f };
|
||||
>c : Symbol(c, Decl(destructuringContextualBindingStackOverflow.ts, 0, 7))
|
||||
>f : Symbol(f, Decl(destructuringContextualBindingStackOverflow.ts, 0, 10))
|
||||
>c : Symbol(c, Decl(destructuringContextualBindingStackOverflow.ts, 0, 44))
|
||||
>f : Symbol(f, Decl(destructuringContextualBindingStackOverflow.ts, 0, 50))
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
//// [tests/cases/compiler/destructuringContextualBindingStackOverflow.ts] ////
|
||||
|
||||
=== destructuringContextualBindingStackOverflow.ts ===
|
||||
const { c, f }: string | number | symbol = { c: 0, f };
|
||||
>c : any
|
||||
> : ^^^
|
||||
>f : any
|
||||
> : ^^^
|
||||
>{ c: 0, f } : { c: number; f: any; }
|
||||
> : ^^^^^^^^^^^^^^^^^^^^^^
|
||||
>c : number
|
||||
> : ^^^^^^
|
||||
>0 : 0
|
||||
> : ^
|
||||
>f : any
|
||||
> : ^^^
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user