diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 14f8356152d83..197964a18cebf 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -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) } @@ -17300,15 +17301,16 @@ 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) } } @@ -17316,6 +17318,16 @@ func (c *Checker) checkDeclarationInitializer(declaration *ast.Node, checkMode C 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() { diff --git a/tsc/testdata/baselines/reference/compiler/fallbackToBindingPatternForTypeInference.errors.txt b/tsc/testdata/baselines/reference/compiler/fallbackToBindingPatternForTypeInference.errors.txt index 9a04ca00910e5..b4bbae1883842 100644 --- a/tsc/testdata/baselines/reference/compiler/fallbackToBindingPatternForTypeInference.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/fallbackToBindingPatternForTypeInference.errors.txt @@ -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(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. \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt index d310258fda658..fdc94e2f4ebfb 100644 --- a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt @@ -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: 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. \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.js b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.js index d06e1cb45fc74..0287336d8bbaf 100644 --- a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.js +++ b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.js @@ -3,9 +3,11 @@ //// [inferredRestTypeFixedOnce.ts] function wrap(_: (...args: Args) => void) {} wrap(({ cancelable } = {}) => {}); +wrap(({ cancelable }) => {}); //// [inferredRestTypeFixedOnce.js] "use strict"; function wrap(_) { } wrap(({ cancelable } = {}) => { }); +wrap(({ cancelable }) => { }); diff --git a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.symbols b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.symbols index 7694f31ddf592..a3b68bfb31637 100644 --- a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.symbols +++ b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.symbols @@ -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)) + diff --git a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.types b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.types index 24cc2fa727e78..2c315fd9057e6 100644 --- a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.types +++ b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.types @@ -13,3 +13,9 @@ wrap(({ cancelable } = {}) => {}); >cancelable : any >{} : {} +wrap(({ cancelable }) => {}); +>wrap(({ cancelable }) => {}) : void +>wrap : (_: (...args: Args) => void) => void +>({ cancelable }) => {} : ({ cancelable }: { cancelable: any; }) => void +>cancelable : any + diff --git a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt index 823bd410ef0cb..047975734ff15 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt @@ -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) ==== @@ -32,7 +51,7 @@ optional = false, ] = []) => {}; -==== /main.ts (3 errors) ==== +==== /main.ts (12 errors) ==== import { checkedArray, checkedObject } from "./checked"; import { uncheckedArray, uncheckedObject } from "./unchecked"; @@ -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'. + } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols index 5542ea8d095bf..0169b372ba32e 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols @@ -114,3 +114,172 @@ typedObject({ required: "value" }); typedArray(["value"]); >typedArray : Symbol(typedArray, Decl(main.ts, 8, 12)) +// Contextual typing must win over padding for parameter defaults (#64974). +type Obj = (input?: { a?: string; b?: number }) => number; +>Obj : Symbol(Obj, Decl(main.ts, 25, 22)) +>input : Symbol(input, Decl(main.ts, 28, 12)) +>a : Symbol(a, Decl(main.ts, 28, 21)) +>b : Symbol(b, Decl(main.ts, 28, 33)) + +export const obj1: Obj = ({ a, b } = {}) => (a ? 1 : 0) + (b ?? 0); +>obj1 : Symbol(obj1, Decl(main.ts, 29, 12)) +>Obj : Symbol(Obj, Decl(main.ts, 25, 22)) +>a : Symbol(a, Decl(main.ts, 29, 27)) +>b : Symbol(b, Decl(main.ts, 29, 30)) +>a : Symbol(a, Decl(main.ts, 29, 27)) +>b : Symbol(b, Decl(main.ts, 29, 30)) + +type Arr = (input?: [string?, number?]) => number; +>Arr : Symbol(Arr, Decl(main.ts, 29, 67)) +>input : Symbol(input, Decl(main.ts, 31, 12)) + +export const arr1: Arr = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); +>arr1 : Symbol(arr1, Decl(main.ts, 32, 12)) +>Arr : Symbol(Arr, Decl(main.ts, 29, 67)) +>a : Symbol(a, Decl(main.ts, 32, 27)) +>b : Symbol(b, Decl(main.ts, 32, 29)) +>a : Symbol(a, Decl(main.ts, 32, 27)) +>b : Symbol(b, Decl(main.ts, 32, 29)) + +export const arr2: (input: [string?, number?]) => number = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); +>arr2 : Symbol(arr2, Decl(main.ts, 34, 12)) +>input : Symbol(input, Decl(main.ts, 34, 20)) +>a : Symbol(a, Decl(main.ts, 34, 61)) +>b : Symbol(b, Decl(main.ts, 34, 63)) +>a : Symbol(a, Decl(main.ts, 34, 61)) +>b : Symbol(b, Decl(main.ts, 34, 63)) + +declare function takeArr(callback: Arr): void; +>takeArr : Symbol(takeArr, Decl(main.ts, 34, 99)) +>callback : Symbol(callback, Decl(main.ts, 36, 25)) +>Arr : Symbol(Arr, Decl(main.ts, 29, 67)) + +takeArr(([a, b] = []) => { +>takeArr : Symbol(takeArr, Decl(main.ts, 34, 99)) +>a : Symbol(a, Decl(main.ts, 37, 10)) +>b : Symbol(b, Decl(main.ts, 37, 12)) + + const c: number = a; +>c : Symbol(c, Decl(main.ts, 38, 9)) +>a : Symbol(a, Decl(main.ts, 37, 10)) + + const d: string = b; +>d : Symbol(d, Decl(main.ts, 39, 9)) +>b : Symbol(b, Decl(main.ts, 37, 12)) + + return 0; +}); + +export const arr3: (input?: [{ a?: number }?]) => void = ([{ a } = {}] = []) => { +>arr3 : Symbol(arr3, Decl(main.ts, 43, 12)) +>input : Symbol(input, Decl(main.ts, 43, 20)) +>a : Symbol(a, Decl(main.ts, 43, 30)) +>a : Symbol(a, Decl(main.ts, 43, 60)) + + const b: string = a; +>b : Symbol(b, Decl(main.ts, 44, 9)) +>a : Symbol(a, Decl(main.ts, 43, 60)) + +}; + +export const arr4: (input?: { a?: number }[]) => void = ([...[{ a } = {}]] = []) => { +>arr4 : Symbol(arr4, Decl(main.ts, 47, 12)) +>input : Symbol(input, Decl(main.ts, 47, 20)) +>a : Symbol(a, Decl(main.ts, 47, 29)) +>a : Symbol(a, Decl(main.ts, 47, 63)) + + const b: string = a; +>b : Symbol(b, Decl(main.ts, 48, 9)) +>a : Symbol(a, Decl(main.ts, 47, 63)) + +}; + +// Annotations and contextual types also type the elements of nested defaults (#64431). +interface Outer { +>Outer : Symbol(Outer, Decl(main.ts, 49, 2)) + + inner?: { a?: number; b?: number }; +>inner : Symbol(Outer.inner, Decl(main.ts, 52, 17)) +>a : Symbol(a, Decl(main.ts, 53, 13)) +>b : Symbol(b, Decl(main.ts, 53, 25)) +} + +export function annotatedNested({ inner: { a = 1, b } = {} }: Outer) { +>annotatedNested : Symbol(annotatedNested, Decl(main.ts, 54, 1)) +>inner : Symbol(Outer.inner, Decl(main.ts, 52, 17)) +>a : Symbol(a, Decl(main.ts, 56, 42)) +>b : Symbol(b, Decl(main.ts, 56, 49)) +>Outer : Symbol(Outer, Decl(main.ts, 49, 2)) + + const c: string = b; +>c : Symbol(c, Decl(main.ts, 57, 9)) +>b : Symbol(b, Decl(main.ts, 56, 49)) +} + +export function annotatedNestedAlone({ inner: { b } = {} }: Outer) { +>annotatedNestedAlone : Symbol(annotatedNestedAlone, Decl(main.ts, 58, 1)) +>inner : Symbol(Outer.inner, Decl(main.ts, 52, 17)) +>b : Symbol(b, Decl(main.ts, 60, 47)) +>Outer : Symbol(Outer, Decl(main.ts, 49, 2)) + + const c: string = b; +>c : Symbol(c, Decl(main.ts, 61, 9)) +>b : Symbol(b, Decl(main.ts, 60, 47)) +} + +declare const items: { result?: { summary: number } }[]; +>items : Symbol(items, Decl(main.ts, 64, 13)) +>result : Symbol(result, Decl(main.ts, 64, 22)) +>summary : Symbol(summary, Decl(main.ts, 64, 33)) + +items.map(({ result: { summary } = {} }) => { +>items.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>items : Symbol(items, Decl(main.ts, 64, 13)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>result : Symbol(result, Decl(main.ts, 64, 22)) +>summary : Symbol(summary, Decl(main.ts, 65, 22)) + + const c: string = summary; +>c : Symbol(c, Decl(main.ts, 66, 9)) +>summary : Symbol(summary, Decl(main.ts, 65, 22)) + +}); + +declare function takeAny(callback: (input: any) => void): void; +>takeAny : Symbol(takeAny, Decl(main.ts, 67, 3)) +>callback : Symbol(callback, Decl(main.ts, 69, 25)) +>input : Symbol(input, Decl(main.ts, 69, 36)) + +takeAny(({ a } = {}) => {}); +>takeAny : Symbol(takeAny, Decl(main.ts, 67, 3)) +>a : Symbol(a, Decl(main.ts, 70, 10)) + +export const partial: (input?: { known?: number }) => void = ({ known, unknown } = {}) => { +>partial : Symbol(partial, Decl(main.ts, 72, 12)) +>input : Symbol(input, Decl(main.ts, 72, 23)) +>known : Symbol(known, Decl(main.ts, 72, 32)) +>known : Symbol(known, Decl(main.ts, 72, 63)) +>unknown : Symbol(unknown, Decl(main.ts, 72, 70)) + + const c: string = known; +>c : Symbol(c, Decl(main.ts, 73, 9)) +>known : Symbol(known, Decl(main.ts, 72, 63)) + +}; + +=== /jsdoc.js === +// @ts-check +/** @typedef {(input: { inner?: { a?: number } }) => void} Nested */ + +/** @type {Nested} */ +export function jsdocNested({ inner: { a } = {} }) { +>jsdocNested : Symbol(jsdocNested, Decl(jsdoc.js, 0, 0)) +>inner : Symbol(inner, Decl(jsdoc.js, 1, 23)) +>a : Symbol(a, Decl(jsdoc.js, 4, 38)) + + /** @type {string} */ + const c = a; +>c : Symbol(c, Decl(jsdoc.js, 6, 9)) +>a : Symbol(a, Decl(jsdoc.js, 4, 38)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types index 1791cbfaebf14..3230df8d58629 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types @@ -153,3 +153,218 @@ typedArray(["value"]); >["value"] : [string] >"value" : "value" +// Contextual typing must win over padding for parameter defaults (#64974). +type Obj = (input?: { a?: string; b?: number }) => number; +>Obj : Obj +>input : { a?: string; b?: number; } | undefined +>a : string | undefined +>b : number | undefined + +export const obj1: Obj = ({ a, b } = {}) => (a ? 1 : 0) + (b ?? 0); +>obj1 : Obj +>({ a, b } = {}) => (a ? 1 : 0) + (b ?? 0) : ({ a, b }?: { a?: string; b?: number; } | undefined) => number +>a : string | undefined +>b : number | undefined +>{} : {} +>(a ? 1 : 0) + (b ?? 0) : number +>(a ? 1 : 0) : 0 | 1 +>a ? 1 : 0 : 0 | 1 +>a : string | undefined +>1 : 1 +>0 : 0 +>(b ?? 0) : number +>b ?? 0 : number +>b : number | undefined +>0 : 0 + +type Arr = (input?: [string?, number?]) => number; +>Arr : Arr +>input : [(string | undefined)?, (number | undefined)?] | undefined + +export const arr1: Arr = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); +>arr1 : Arr +>([a, b] = []) => (a ? 1 : 0) + (b ?? 0) : ([a, b]?: [(string | undefined)?, (number | undefined)?] | undefined) => number +>a : string | undefined +>b : number | undefined +>[] : [] +>(a ? 1 : 0) + (b ?? 0) : number +>(a ? 1 : 0) : 0 | 1 +>a ? 1 : 0 : 0 | 1 +>a : string | undefined +>1 : 1 +>0 : 0 +>(b ?? 0) : number +>b ?? 0 : number +>b : number | undefined +>0 : 0 + +export const arr2: (input: [string?, number?]) => number = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); +>arr2 : (input: [string?, number?]) => number +>input : [(string | undefined)?, (number | undefined)?] +>([a, b] = []) => (a ? 1 : 0) + (b ?? 0) : ([a, b]?: [(string | undefined)?, (number | undefined)?]) => number +>a : string | undefined +>b : number | undefined +>[] : [] +>(a ? 1 : 0) + (b ?? 0) : number +>(a ? 1 : 0) : 0 | 1 +>a ? 1 : 0 : 0 | 1 +>a : string | undefined +>1 : 1 +>0 : 0 +>(b ?? 0) : number +>b ?? 0 : number +>b : number | undefined +>0 : 0 + +declare function takeArr(callback: Arr): void; +>takeArr : (callback: Arr) => void +>callback : Arr + +takeArr(([a, b] = []) => { +>takeArr(([a, b] = []) => { const c: number = a; const d: string = b; return 0;}) : void +>takeArr : (callback: Arr) => void +>([a, b] = []) => { const c: number = a; const d: string = b; return 0;} : ([a, b]?: [(string | undefined)?, (number | undefined)?] | undefined) => number +>a : string | undefined +>b : number | undefined +>[] : [] + + const c: number = a; +>c : number +>a : string | undefined + + const d: string = b; +>d : string +>b : number | undefined + + return 0; +>0 : 0 + +}); + +export const arr3: (input?: [{ a?: number }?]) => void = ([{ a } = {}] = []) => { +>arr3 : (input?: [{ a?: number; }?]) => void +>input : [({ a?: number; } | undefined)?] | undefined +>a : number | undefined +>([{ a } = {}] = []) => { const b: string = a;} : ([{ a }]?: [({ a?: number; } | undefined)?] | undefined) => void +>a : number | undefined +>{} : {} +>[] : [] + + const b: string = a; +>b : string +>a : number | undefined + +}; + +export const arr4: (input?: { a?: number }[]) => void = ([...[{ a } = {}]] = []) => { +>arr4 : (input?: { a?: number; }[]) => void +>input : { a?: number; }[] | undefined +>a : number | undefined +>([...[{ a } = {}]] = []) => { const b: string = a;} : ([...[{ a }]]?: { a?: number; }[] | undefined) => void +>a : number | undefined +>{} : {} +>[] : never[] + + const b: string = a; +>b : string +>a : number | undefined + +}; + +// Annotations and contextual types also type the elements of nested defaults (#64431). +interface Outer { + inner?: { a?: number; b?: number }; +>inner : { a?: number; b?: number; } | undefined +>a : number | undefined +>b : number | undefined +} + +export function annotatedNested({ inner: { a = 1, b } = {} }: Outer) { +>annotatedNested : ({ inner: { a, b } }: Outer) => void +>inner : any +>a : number +>1 : 1 +>b : number | undefined +>{} : {} + + const c: string = b; +>c : string +>b : number | undefined +} + +export function annotatedNestedAlone({ inner: { b } = {} }: Outer) { +>annotatedNestedAlone : ({ inner: { b } }: Outer) => void +>inner : any +>b : number | undefined +>{} : {} + + const c: string = b; +>c : string +>b : number | undefined +} + +declare const items: { result?: { summary: number } }[]; +>items : { result?: { summary: number; }; }[] +>result : { summary: number; } | undefined +>summary : number + +items.map(({ result: { summary } = {} }) => { +>items.map(({ result: { summary } = {} }) => { const c: string = summary;}) : void[] +>items.map : (callbackfn: (value: { result?: { summary: number; }; }, index: number, array: { result?: { summary: number; }; }[]) => U, thisArg?: any) => U[] +>items : { result?: { summary: number; }; }[] +>map : (callbackfn: (value: { result?: { summary: number; }; }, index: number, array: { result?: { summary: number; }; }[]) => U, thisArg?: any) => U[] +>({ result: { summary } = {} }) => { const c: string = summary;} : ({ result: { summary } }: { result?: { summary: number; }; }) => void +>result : any +>summary : number | undefined +>{} : {} + + const c: string = summary; +>c : string +>summary : number | undefined + +}); + +declare function takeAny(callback: (input: any) => void): void; +>takeAny : (callback: (input: any) => void) => void +>callback : (input: any) => void +>input : any + +takeAny(({ a } = {}) => {}); +>takeAny(({ a } = {}) => {}) : void +>takeAny : (callback: (input: any) => void) => void +>({ a } = {}) => {} : ({ a }?: any) => void +>a : any +>{} : {} + +export const partial: (input?: { known?: number }) => void = ({ known, unknown } = {}) => { +>partial : (input?: { known?: number; }) => void +>input : { known?: number; } | undefined +>known : number | undefined +>({ known, unknown } = {}) => { const c: string = known;} : ({ known, unknown }?: { known?: number; } | undefined) => void +>known : number | undefined +>unknown : any +>{} : {} + + const c: string = known; +>c : string +>known : number | undefined + +}; + +=== /jsdoc.js === +// @ts-check +/** @typedef {(input: { inner?: { a?: number } }) => void} Nested */ + +/** @type {Nested} */ +export function jsdocNested({ inner: { a } = {} }) { +>jsdocNested : (input: { inner?: { a?: number; }; }) => void +>inner : any +>a : number | undefined +>{} : {} + + /** @type {string} */ + const c = a; +>c : string +>a : number | undefined +} + diff --git a/tsc/testdata/tests/cases/compiler/inferredRestTypeFixedOnce.ts b/tsc/testdata/tests/cases/compiler/inferredRestTypeFixedOnce.ts index f69a3380be473..aab3134217947 100644 --- a/tsc/testdata/tests/cases/compiler/inferredRestTypeFixedOnce.ts +++ b/tsc/testdata/tests/cases/compiler/inferredRestTypeFixedOnce.ts @@ -1,3 +1,4 @@ // @target: es2015 function wrap(_: (...args: Args) => void) {} wrap(({ cancelable } = {}) => {}); +wrap(({ cancelable }) => {}); diff --git a/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts b/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts index e0ea93281bfc9..9dfe03ba6897d 100644 --- a/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts +++ b/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts @@ -53,3 +53,62 @@ checkedObject({ required: "value" }); 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; + const d: string = b; + return 0; +}); + +export const arr3: (input?: [{ a?: number }?]) => void = ([{ a } = {}] = []) => { + const b: string = a; +}; + +export const arr4: (input?: { a?: number }[]) => void = ([...[{ a } = {}]] = []) => { + const b: string = a; +}; + +// 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; +} + +export function annotatedNestedAlone({ inner: { b } = {} }: Outer) { + const c: string = b; +} + +declare const items: { result?: { summary: number } }[]; +items.map(({ result: { summary } = {} }) => { + const c: string = summary; +}); + +declare function takeAny(callback: (input: any) => void): void; +takeAny(({ a } = {}) => {}); + +export const partial: (input?: { known?: number }) => void = ({ known, unknown } = {}) => { + const c: string = known; +}; + +// @filename: /jsdoc.js +// @ts-check +/** @typedef {(input: { inner?: { a?: number } }) => void} Nested */ + +/** @type {Nested} */ +export function jsdocNested({ inner: { a } = {} }) { + /** @type {string} */ + const c = a; +}