From 0599fbd387d9abc9852e6336e47e3861bc55c071 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:03:31 -0800 Subject: [PATCH 1/2] Test parameter defaults typed by annotations or context Padding a destructured parameter's default with its missing binding elements adds them as implicitly any, even when an annotation, a JSDoc @type signature, or a contextual type already types the parameter. That reports implicit any on elements that have types (64431, 64974, 65011), and under a contextual type a nested default's elements become any. Tuple defaults contextually typed by a tuple type have long reported the same errors. --- ...ngPatternDefaultMissingElements.errors.txt | 369 ++++++++++++- ...ndingPatternDefaultMissingElements.symbols | 439 +++++++++++++++ ...BindingPatternDefaultMissingElements.types | 519 ++++++++++++++++++ ...ectBindingPatternDefaultMissingElements.ts | 150 +++++ 4 files changed, 1476 insertions(+), 1 deletion(-) diff --git a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt index 823bd410ef0cb..b042da9fab1c4 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt @@ -1,8 +1,87 @@ /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(5,40): error TS7031: Binding element 'a' implicitly has an 'any' type. +/jsdoc.js(16,27): error TS7031: Binding element 'a' implicitly has an 'any' type. +/jsdoc.js(18,19): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/jsdoc.js(26,58): error TS7031: Binding element 'a' implicitly has an 'any' type. +/jsdoc.js(28,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(30,29): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(30,32): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(33,28): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(33,31): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(35,62): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(35,65): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(38,11): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(38,14): error TS7031: Binding element 'b' 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(44,62): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(48,65): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(57,51): error TS7031: Binding element 'b' implicitly has an 'any' type. +/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(61,49): error TS7031: Binding element 'b' implicitly has an 'any' type. +/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(66,24): error TS7031: Binding element 'summary' implicitly has an 'any' type. +/main.ts(71,12): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(73,65): error TS7031: Binding element 'known' implicitly has an 'any' type. +/main.ts(73,72): error TS2339: Property 'unknown' does not exist on type '{ known?: number | undefined; }'. +/main.ts(73,72): error TS7031: Binding element 'unknown' implicitly has an 'any' type. +/main.ts(74,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(77,76): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(77,79): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(78,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. + Type 'undefined' is not assignable to type 'number'. +/main.ts(79,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(83,15): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(83,18): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(84,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. + Type 'undefined' is not assignable to type 'number'. +/main.ts(85,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(92,18): error TS7031: Binding element 'recursive' implicitly has an 'any' type. +/main.ts(92,29): error TS7031: Binding element 'force' implicitly has an 'any' type. +/main.ts(93,15): error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(94,15): error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(98,91): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(102,55): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(103,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(107,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. + Type 'undefined' is not assignable to type 'number'. +/main.ts(115,11): error TS2322: Type 'number' is not assignable to type 'string'. +/main.ts(118,79): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(119,11): error TS2322: Type 'number' is not assignable to type 'string'. +/main.ts(122,57): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(127,22): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(128,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(131,83): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(132,11): error TS2322: Type 'boolean' is not assignable to type 'string'. +/main.ts(133,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. + Type 'undefined' is not assignable to type 'number'. +/main.ts(137,18): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(137,21): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(138,15): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. +/main.ts(139,15): error TS2322: Type 'string | undefined' is not assignable to type 'number'. + Type 'undefined' is not assignable to type 'number'. +/main.ts(142,101): error TS7031: Binding element 'a' implicitly has an 'any' type. +/main.ts(143,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. + Type 'undefined' is not assignable to type 'number'. +/main.ts(144,11): error TS2322: Type 'number' is not assignable to type 'string'. ==== /unchecked.js (0 errors) ==== @@ -32,7 +111,7 @@ optional = false, ] = []) => {}; -==== /main.ts (3 errors) ==== +==== /main.ts (57 errors) ==== import { checkedArray, checkedObject } from "./checked"; import { uncheckedArray, uncheckedObject } from "./unchecked"; @@ -65,4 +144,292 @@ 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); + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + + type Arr = (input?: [string?, number?]) => number; + export const arr1: Arr = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + + export const arr2: (input: [string?, number?]) => number = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + + declare function takeArr(callback: Arr): void; + takeArr(([a, b] = []) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + 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 } = {}] = []) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + const b: string = a; + }; + + export const arr4: (input?: { a?: number }[]) => void = ([...[{ a } = {}]] = []) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + 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) { + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + 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) { + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + 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 } = {} }) => { + ~~~~~~~ +!!! error TS7031: Binding element 'summary' implicitly has an 'any' type. + const c: string = summary; + }); + + declare function takeAny(callback: (input: any) => void): void; + takeAny(({ a } = {}) => {}); + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + + export const partial: (input?: { known?: number }) => void = ({ known, unknown } = {}) => { + ~~~~~ +!!! error TS7031: Binding element 'known' implicitly has an 'any' type. + ~~~~~~~ +!!! error TS2339: Property 'unknown' does not exist on type '{ known?: number | undefined; }'. + ~~~~~~~ +!!! error TS7031: Binding element 'unknown' implicitly has an 'any' type. + 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'. + }; + + export const nonOptional: (input: { a?: string; b?: number }) => void = ({ a, b } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + 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'. + }; + + declare function takeObject(callback: (input?: { a?: string; b?: number }) => void): void; + takeObject(({ a, b } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + 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'. + }); + + interface Driver { + rm(path: string, options?: { recursive?: boolean; force?: boolean }): void; + } + export const driver: Driver = { + rm: (path, { recursive, force } = {}) => { + ~~~~~~~~~ +!!! error TS7031: Binding element 'recursive' implicitly has an 'any' type. + ~~~~~ +!!! error TS7031: Binding element 'force' implicitly has an 'any' type. + const c: string = recursive; + ~ +!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. +!!! error TS2322: Type 'undefined' is not assignable to type 'string'. + const d: string = force; + ~ +!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. +!!! error TS2322: Type 'undefined' is not assignable to type 'string'. + }, + }; + + export const contextualNested: (input?: { inner?: { a?: number } }) => void = ({ inner: { a } = {} } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + const c: string = a; + }; + + export function annotatedNestedWithDefault({ inner: { b } = {} }: Outer = {}) { + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + 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 annotatedWithDefault({ a, b }: { a?: string; b?: number } = {}) { + 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'. + } + + interface OuterArray { + inner?: number[]; + } + + export function annotatedNestedArray({ inner: [a = 1, b] = [] }: OuterArray) { + const c: string = b; + ~ +!!! error TS2322: Type 'number' is not assignable to type 'string'. + } + + export const indexSignature: (input?: { [key: string]: number }) => void = ({ a } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + const c: string = a; + ~ +!!! error TS2322: Type 'number' is not assignable to type 'string'. + }; + + export const nestedAny: (input?: any) => void = ({ a: { b } = {} } = {}) => { + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + b; + }; + + declare function generic(value: T, callback: (input?: Partial) => void): void; + generic({ a: 1 }, ({ a } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + const c: 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 nonEmptyDefault: (input: { a: boolean; b?: string }) => void = ({ a, b } = { a: false }) => { + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + const c: string = a; + ~ +!!! error TS2322: Type 'boolean' is not assignable to type 'string'. + const d: number = b; + ~ +!!! error TS2322: Type 'string | undefined' is not assignable to type 'number'. +!!! error TS2322: Type 'undefined' is not assignable to type 'number'. + }; + + export const mapped = (input: { a?: number; b?: string }[]) => + input.map(({ a, b } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + ~ +!!! error TS7031: Binding element 'b' implicitly has an 'any' type. + const c: string = a; + ~ +!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. +!!! error TS2322: Type 'undefined' is not assignable to type 'string'. + const d: number = b; + ~ +!!! error TS2322: Type 'string | undefined' is not assignable to type 'number'. +!!! error TS2322: Type 'undefined' is not assignable to type 'number'. + }); + + export const mixedDefaults: (args: string[], input?: { a?: string; b?: number }) => void = (args, { a, b = 1 } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + 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' is not assignable to type 'string'. + }; + +==== /jsdoc.js (5 errors) ==== + // @ts-check + /** @typedef {(input: { inner?: { a?: number } }) => void} Nested */ + + /** @type {Nested} */ + export function jsdocNested({ inner: { a } = {} }) { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + /** @type {string} */ + const c = a; + } + + /** @typedef {{ a?: number }} Options */ + /** @typedef {{ method: (b: string, c?: Options) => Promise }} Container */ + + /** @returns {Container} */ + export function returnsContainer() { + return { + async method(b, { a } = {}) { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + /** @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'. + return [b]; + }, + }; + } + + /** @typedef {(b: number, c?: Options) => Promise} Callback */ + + export const cast = /** @type {Callback} */ (async (b, { a } = {}) => { + ~ +!!! error TS7031: Binding element 'a' implicitly has an 'any' type. + /** @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..6ec8a2c425eaa 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols @@ -114,3 +114,442 @@ 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)) + +}; + +export const nonOptional: (input: { a?: string; b?: number }) => void = ({ a, b } = {}) => { +>nonOptional : Symbol(nonOptional, Decl(main.ts, 76, 12)) +>input : Symbol(input, Decl(main.ts, 76, 27)) +>a : Symbol(a, Decl(main.ts, 76, 35)) +>b : Symbol(b, Decl(main.ts, 76, 47)) +>a : Symbol(a, Decl(main.ts, 76, 74)) +>b : Symbol(b, Decl(main.ts, 76, 77)) + + const c: number = a; +>c : Symbol(c, Decl(main.ts, 77, 9)) +>a : Symbol(a, Decl(main.ts, 76, 74)) + + const d: string = b; +>d : Symbol(d, Decl(main.ts, 78, 9)) +>b : Symbol(b, Decl(main.ts, 76, 77)) + +}; + +declare function takeObject(callback: (input?: { a?: string; b?: number }) => void): void; +>takeObject : Symbol(takeObject, Decl(main.ts, 79, 2)) +>callback : Symbol(callback, Decl(main.ts, 81, 28)) +>input : Symbol(input, Decl(main.ts, 81, 39)) +>a : Symbol(a, Decl(main.ts, 81, 48)) +>b : Symbol(b, Decl(main.ts, 81, 60)) + +takeObject(({ a, b } = {}) => { +>takeObject : Symbol(takeObject, Decl(main.ts, 79, 2)) +>a : Symbol(a, Decl(main.ts, 82, 13)) +>b : Symbol(b, Decl(main.ts, 82, 16)) + + const c: number = a; +>c : Symbol(c, Decl(main.ts, 83, 9)) +>a : Symbol(a, Decl(main.ts, 82, 13)) + + const d: string = b; +>d : Symbol(d, Decl(main.ts, 84, 9)) +>b : Symbol(b, Decl(main.ts, 82, 16)) + +}); + +interface Driver { +>Driver : Symbol(Driver, Decl(main.ts, 85, 3)) + + rm(path: string, options?: { recursive?: boolean; force?: boolean }): void; +>rm : Symbol(Driver.rm, Decl(main.ts, 87, 18)) +>path : Symbol(path, Decl(main.ts, 88, 7)) +>options : Symbol(options, Decl(main.ts, 88, 20)) +>recursive : Symbol(recursive, Decl(main.ts, 88, 32)) +>force : Symbol(force, Decl(main.ts, 88, 53)) +} +export const driver: Driver = { +>driver : Symbol(driver, Decl(main.ts, 90, 12)) +>Driver : Symbol(Driver, Decl(main.ts, 85, 3)) + + rm: (path, { recursive, force } = {}) => { +>rm : Symbol(rm, Decl(main.ts, 90, 31)) +>path : Symbol(path, Decl(main.ts, 91, 9)) +>recursive : Symbol(recursive, Decl(main.ts, 91, 16)) +>force : Symbol(force, Decl(main.ts, 91, 27)) + + const c: string = recursive; +>c : Symbol(c, Decl(main.ts, 92, 13)) +>recursive : Symbol(recursive, Decl(main.ts, 91, 16)) + + const d: string = force; +>d : Symbol(d, Decl(main.ts, 93, 13)) +>force : Symbol(force, Decl(main.ts, 91, 27)) + + }, +}; + +export const contextualNested: (input?: { inner?: { a?: number } }) => void = ({ inner: { a } = {} } = {}) => { +>contextualNested : Symbol(contextualNested, Decl(main.ts, 97, 12)) +>input : Symbol(input, Decl(main.ts, 97, 32)) +>inner : Symbol(inner, Decl(main.ts, 97, 41)) +>a : Symbol(a, Decl(main.ts, 97, 51)) +>a : Symbol(a, Decl(main.ts, 97, 89)) + + const c: string = a; +>c : Symbol(c, Decl(main.ts, 98, 9)) +>a : Symbol(a, Decl(main.ts, 97, 89)) + +}; + +export function annotatedNestedWithDefault({ inner: { b } = {} }: Outer = {}) { +>annotatedNestedWithDefault : Symbol(annotatedNestedWithDefault, Decl(main.ts, 99, 2)) +>inner : Symbol(Outer.inner, Decl(main.ts, 52, 17)) +>b : Symbol(b, Decl(main.ts, 101, 53)) +>Outer : Symbol(Outer, Decl(main.ts, 49, 2)) + + const c: string = b; +>c : Symbol(c, Decl(main.ts, 102, 9)) +>b : Symbol(b, Decl(main.ts, 101, 53)) +} + +export function annotatedWithDefault({ a, b }: { a?: string; b?: number } = {}) { +>annotatedWithDefault : Symbol(annotatedWithDefault, Decl(main.ts, 103, 1)) +>a : Symbol(a, Decl(main.ts, 105, 38)) +>b : Symbol(b, Decl(main.ts, 105, 41)) +>a : Symbol(a, Decl(main.ts, 105, 48)) +>b : Symbol(b, Decl(main.ts, 105, 60)) + + const c: number = a; +>c : Symbol(c, Decl(main.ts, 106, 9)) +>a : Symbol(a, Decl(main.ts, 105, 38)) +} + +interface OuterArray { +>OuterArray : Symbol(OuterArray, Decl(main.ts, 107, 1)) + + inner?: number[]; +>inner : Symbol(OuterArray.inner, Decl(main.ts, 109, 22)) +} + +export function annotatedNestedArray({ inner: [a = 1, b] = [] }: OuterArray) { +>annotatedNestedArray : Symbol(annotatedNestedArray, Decl(main.ts, 111, 1)) +>inner : Symbol(OuterArray.inner, Decl(main.ts, 109, 22)) +>a : Symbol(a, Decl(main.ts, 113, 47)) +>b : Symbol(b, Decl(main.ts, 113, 53)) +>OuterArray : Symbol(OuterArray, Decl(main.ts, 107, 1)) + + const c: string = b; +>c : Symbol(c, Decl(main.ts, 114, 9)) +>b : Symbol(b, Decl(main.ts, 113, 53)) +} + +export const indexSignature: (input?: { [key: string]: number }) => void = ({ a } = {}) => { +>indexSignature : Symbol(indexSignature, Decl(main.ts, 117, 12)) +>input : Symbol(input, Decl(main.ts, 117, 30)) +>key : Symbol(key, Decl(main.ts, 117, 41)) +>a : Symbol(a, Decl(main.ts, 117, 77)) + + const c: string = a; +>c : Symbol(c, Decl(main.ts, 118, 9)) +>a : Symbol(a, Decl(main.ts, 117, 77)) + +}; + +export const nestedAny: (input?: any) => void = ({ a: { b } = {} } = {}) => { +>nestedAny : Symbol(nestedAny, Decl(main.ts, 121, 12)) +>input : Symbol(input, Decl(main.ts, 121, 25)) +>b : Symbol(b, Decl(main.ts, 121, 55)) + + b; +>b : Symbol(b, Decl(main.ts, 121, 55)) + +}; + +declare function generic(value: T, callback: (input?: Partial) => void): void; +>generic : Symbol(generic, Decl(main.ts, 123, 2)) +>T : Symbol(T, Decl(main.ts, 125, 25)) +>value : Symbol(value, Decl(main.ts, 125, 28)) +>T : Symbol(T, Decl(main.ts, 125, 25)) +>callback : Symbol(callback, Decl(main.ts, 125, 37)) +>input : Symbol(input, Decl(main.ts, 125, 49)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(main.ts, 125, 25)) + +generic({ a: 1 }, ({ a } = {}) => { +>generic : Symbol(generic, Decl(main.ts, 123, 2)) +>a : Symbol(a, Decl(main.ts, 126, 9)) +>a : Symbol(a, Decl(main.ts, 126, 20)) + + const c: string = a; +>c : Symbol(c, Decl(main.ts, 127, 9)) +>a : Symbol(a, Decl(main.ts, 126, 20)) + +}); + +export const nonEmptyDefault: (input: { a: boolean; b?: string }) => void = ({ a, b } = { a: false }) => { +>nonEmptyDefault : Symbol(nonEmptyDefault, Decl(main.ts, 130, 12)) +>input : Symbol(input, Decl(main.ts, 130, 31)) +>a : Symbol(a, Decl(main.ts, 130, 39)) +>b : Symbol(b, Decl(main.ts, 130, 51)) +>a : Symbol(a, Decl(main.ts, 130, 78)) +>b : Symbol(b, Decl(main.ts, 130, 81)) +>a : Symbol(a, Decl(main.ts, 130, 89)) + + const c: string = a; +>c : Symbol(c, Decl(main.ts, 131, 9)) +>a : Symbol(a, Decl(main.ts, 130, 78)) + + const d: number = b; +>d : Symbol(d, Decl(main.ts, 132, 9)) +>b : Symbol(b, Decl(main.ts, 130, 81)) + +}; + +export const mapped = (input: { a?: number; b?: string }[]) => +>mapped : Symbol(mapped, Decl(main.ts, 135, 12)) +>input : Symbol(input, Decl(main.ts, 135, 23)) +>a : Symbol(a, Decl(main.ts, 135, 31)) +>b : Symbol(b, Decl(main.ts, 135, 43)) + + input.map(({ a, b } = {}) => { +>input.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>input : Symbol(input, Decl(main.ts, 135, 23)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>a : Symbol(a, Decl(main.ts, 136, 16)) +>b : Symbol(b, Decl(main.ts, 136, 19)) + + const c: string = a; +>c : Symbol(c, Decl(main.ts, 137, 13)) +>a : Symbol(a, Decl(main.ts, 136, 16)) + + const d: number = b; +>d : Symbol(d, Decl(main.ts, 138, 13)) +>b : Symbol(b, Decl(main.ts, 136, 19)) + + }); + +export const mixedDefaults: (args: string[], input?: { a?: string; b?: number }) => void = (args, { a, b = 1 } = {}) => { +>mixedDefaults : Symbol(mixedDefaults, Decl(main.ts, 141, 12)) +>args : Symbol(args, Decl(main.ts, 141, 29)) +>input : Symbol(input, Decl(main.ts, 141, 44)) +>a : Symbol(a, Decl(main.ts, 141, 54)) +>b : Symbol(b, Decl(main.ts, 141, 66)) +>args : Symbol(args, Decl(main.ts, 141, 92)) +>a : Symbol(a, Decl(main.ts, 141, 99)) +>b : Symbol(b, Decl(main.ts, 141, 102)) + + const c: number = a; +>c : Symbol(c, Decl(main.ts, 142, 9)) +>a : Symbol(a, Decl(main.ts, 141, 99)) + + const d: string = b; +>d : Symbol(d, Decl(main.ts, 143, 9)) +>b : Symbol(b, Decl(main.ts, 141, 102)) + +}; + +=== /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)) +} + +/** @typedef {{ a?: number }} Options */ +/** @typedef {{ method: (b: string, c?: Options) => Promise }} Container */ + +/** @returns {Container} */ +export function returnsContainer() { +>returnsContainer : Symbol(returnsContainer, Decl(jsdoc.js, 7, 1)) + + return { + async method(b, { a } = {}) { +>method : Symbol(method, Decl(jsdoc.js, 14, 12)) +>b : Symbol(b, Decl(jsdoc.js, 15, 21)) +>a : Symbol(a, Decl(jsdoc.js, 15, 25)) + + /** @type {string} */ + const c = a; +>c : Symbol(c, Decl(jsdoc.js, 17, 17)) +>a : Symbol(a, Decl(jsdoc.js, 15, 25)) + + return [b]; +>b : Symbol(b, Decl(jsdoc.js, 15, 21)) + + }, + }; +} + +/** @typedef {(b: number, c?: Options) => Promise} Callback */ + +export const cast = /** @type {Callback} */ (async (b, { a } = {}) => { +>cast : Symbol(cast, Decl(jsdoc.js, 25, 12)) +>b : Symbol(b, Decl(jsdoc.js, 25, 52)) +>a : Symbol(a, Decl(jsdoc.js, 25, 56)) + + /** @type {string} */ + const c = a; +>c : Symbol(c, Decl(jsdoc.js, 27, 9)) +>a : Symbol(a, Decl(jsdoc.js, 25, 56)) + +}); + diff --git a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types index 1791cbfaebf14..dc5b6a3758eda 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types @@ -153,3 +153,522 @@ 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 : any +>{} : {} +>[] : [] + + const b: string = a; +>b : string +>a : any + +}; + +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 : any +>{} : {} +>[] : never[] + + const b: string = a; +>b : string +>a : any + +}; + +// 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 : any +>{} : {} + + const c: string = summary; +>c : string +>summary : any + +}); + +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 + +}; + +export const nonOptional: (input: { a?: string; b?: number }) => void = ({ a, b } = {}) => { +>nonOptional : (input: { a?: string; b?: number; }) => void +>input : { a?: string; b?: number; } +>a : string | undefined +>b : number | undefined +>({ a, b } = {}) => { const c: number = a; const d: string = b;} : ({ a, b }?: { a?: string; b?: number; }) => void +>a : string | undefined +>b : number | undefined +>{} : {} + + const c: number = a; +>c : number +>a : string | undefined + + const d: string = b; +>d : string +>b : number | undefined + +}; + +declare function takeObject(callback: (input?: { a?: string; b?: number }) => void): void; +>takeObject : (callback: (input?: { a?: string; b?: number; }) => void) => void +>callback : (input?: { a?: string; b?: number; }) => void +>input : { a?: string; b?: number; } | undefined +>a : string | undefined +>b : number | undefined + +takeObject(({ a, b } = {}) => { +>takeObject(({ a, b } = {}) => { const c: number = a; const d: string = b;}) : void +>takeObject : (callback: (input?: { a?: string; b?: number; }) => void) => void +>({ a, b } = {}) => { const c: number = a; const d: string = b;} : ({ a, b }?: { a?: string; b?: number; } | undefined) => void +>a : string | undefined +>b : number | undefined +>{} : {} + + const c: number = a; +>c : number +>a : string | undefined + + const d: string = b; +>d : string +>b : number | undefined + +}); + +interface Driver { + rm(path: string, options?: { recursive?: boolean; force?: boolean }): void; +>rm : (path: string, options?: { recursive?: boolean; force?: boolean; }) => void +>path : string +>options : { recursive?: boolean; force?: boolean; } | undefined +>recursive : boolean | undefined +>force : boolean | undefined +} +export const driver: Driver = { +>driver : Driver +>{ rm: (path, { recursive, force } = {}) => { const c: string = recursive; const d: string = force; },} : { rm: (path: string, { recursive, force }?: { recursive?: boolean; force?: boolean; } | undefined) => void; } + + rm: (path, { recursive, force } = {}) => { +>rm : (path: string, { recursive, force }?: { recursive?: boolean; force?: boolean; } | undefined) => void +>(path, { recursive, force } = {}) => { const c: string = recursive; const d: string = force; } : (path: string, { recursive, force }?: { recursive?: boolean; force?: boolean; } | undefined) => void +>path : string +>recursive : boolean | undefined +>force : boolean | undefined +>{} : {} + + const c: string = recursive; +>c : string +>recursive : boolean | undefined + + const d: string = force; +>d : string +>force : boolean | undefined + + }, +}; + +export const contextualNested: (input?: { inner?: { a?: number } }) => void = ({ inner: { a } = {} } = {}) => { +>contextualNested : (input?: { inner?: { a?: number; }; }) => void +>input : { inner?: { a?: number; }; } | undefined +>inner : { a?: number; } | undefined +>a : number | undefined +>({ inner: { a } = {} } = {}) => { const c: string = a;} : ({ inner: { a } }?: { inner?: { a?: number; }; } | undefined) => void +>inner : any +>a : any +>{} : {} +>{} : {} + + const c: string = a; +>c : string +>a : any + +}; + +export function annotatedNestedWithDefault({ inner: { b } = {} }: Outer = {}) { +>annotatedNestedWithDefault : ({ inner: { b } }?: Outer) => void +>inner : any +>b : number | undefined +>{} : {} +>{} : {} + + const c: string = b; +>c : string +>b : number | undefined +} + +export function annotatedWithDefault({ a, b }: { a?: string; b?: number } = {}) { +>annotatedWithDefault : ({ a, b }?: { a?: string; b?: number; }) => void +>a : string | undefined +>b : number | undefined +>a : string | undefined +>b : number | undefined +>{} : {} + + const c: number = a; +>c : number +>a : string | undefined +} + +interface OuterArray { + inner?: number[]; +>inner : number[] | undefined +} + +export function annotatedNestedArray({ inner: [a = 1, b] = [] }: OuterArray) { +>annotatedNestedArray : ({ inner: [a, b] }: OuterArray) => void +>inner : any +>a : number +>1 : 1 +>b : number +>[] : never[] + + const c: string = b; +>c : string +>b : number +} + +export const indexSignature: (input?: { [key: string]: number }) => void = ({ a } = {}) => { +>indexSignature : (input?: { [key: string]: number; }) => void +>input : { [key: string]: number; } | undefined +>key : string +>({ a } = {}) => { const c: string = a;} : ({ a }?: { [key: string]: number; } | undefined) => void +>a : number +>{} : {} + + const c: string = a; +>c : string +>a : number + +}; + +export const nestedAny: (input?: any) => void = ({ a: { b } = {} } = {}) => { +>nestedAny : (input?: any) => void +>input : any +>({ a: { b } = {} } = {}) => { b;} : ({ a: { b } }?: any) => void +>a : any +>b : any +>{} : {} +>{} : {} + + b; +>b : any + +}; + +declare function generic(value: T, callback: (input?: Partial) => void): void; +>generic : (value: T, callback: (input?: Partial) => void) => void +>value : T +>callback : (input?: Partial) => void +>input : Partial | undefined + +generic({ a: 1 }, ({ a } = {}) => { +>generic({ a: 1 }, ({ a } = {}) => { const c: string = a;}) : void +>generic : (value: T, callback: (input?: Partial) => void) => void +>{ a: 1 } : { a: number; } +>a : number +>1 : 1 +>({ a } = {}) => { const c: string = a;} : ({ a }?: Partial<{ a: number; }> | undefined) => void +>a : number | undefined +>{} : {} + + const c: string = a; +>c : string +>a : number | undefined + +}); + +export const nonEmptyDefault: (input: { a: boolean; b?: string }) => void = ({ a, b } = { a: false }) => { +>nonEmptyDefault : (input: { a: boolean; b?: string; }) => void +>input : { a: boolean; b?: string; } +>a : boolean +>b : string | undefined +>({ a, b } = { a: false }) => { const c: string = a; const d: number = b;} : ({ a, b }?: { a: boolean; b?: string; }) => void +>a : boolean +>b : string | undefined +>{ a: false } : { a: false; } +>a : false +>false : false + + const c: string = a; +>c : string +>a : boolean + + const d: number = b; +>d : number +>b : string | undefined + +}; + +export const mapped = (input: { a?: number; b?: string }[]) => +>mapped : (input: { a?: number; b?: string; }[]) => void[] +>(input: { a?: number; b?: string }[]) => input.map(({ a, b } = {}) => { const c: string = a; const d: number = b; }) : (input: { a?: number; b?: string; }[]) => void[] +>input : { a?: number; b?: string; }[] +>a : number | undefined +>b : string | undefined + + input.map(({ a, b } = {}) => { +>input.map(({ a, b } = {}) => { const c: string = a; const d: number = b; }) : void[] +>input.map : (callbackfn: (value: { a?: number; b?: string; }, index: number, array: { a?: number; b?: string; }[]) => U, thisArg?: any) => U[] +>input : { a?: number; b?: string; }[] +>map : (callbackfn: (value: { a?: number; b?: string; }, index: number, array: { a?: number; b?: string; }[]) => U, thisArg?: any) => U[] +>({ a, b } = {}) => { const c: string = a; const d: number = b; } : ({ a, b }?: { a?: number; b?: string; }) => void +>a : number | undefined +>b : string | undefined +>{} : {} + + const c: string = a; +>c : string +>a : number | undefined + + const d: number = b; +>d : number +>b : string | undefined + + }); + +export const mixedDefaults: (args: string[], input?: { a?: string; b?: number }) => void = (args, { a, b = 1 } = {}) => { +>mixedDefaults : (args: string[], input?: { a?: string; b?: number; }) => void +>args : string[] +>input : { a?: string; b?: number; } | undefined +>a : string | undefined +>b : number | undefined +>(args, { a, b = 1 } = {}) => { const c: number = a; const d: string = b;} : (args: string[], { a, b }?: { a?: string; b?: number; } | undefined) => void +>args : string[] +>a : string | undefined +>b : number +>1 : 1 +>{} : {} + + const c: number = a; +>c : number +>a : string | undefined + + const d: string = b; +>d : string +>b : number + +}; + +=== /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 : any +>{} : {} + + /** @type {string} */ + const c = a; +>c : string +>a : any +} + +/** @typedef {{ a?: number }} Options */ +/** @typedef {{ method: (b: string, c?: Options) => Promise }} Container */ + +/** @returns {Container} */ +export function returnsContainer() { +>returnsContainer : () => Container + + return { +>{ async method(b, { a } = {}) { /** @type {string} */ const c = a; return [b]; }, } : { method(b: string, { a }?: Options | undefined): Promise; } + + async method(b, { a } = {}) { +>method : (b: string, { a }?: Options | undefined) => Promise +>b : string +>a : number | undefined +>{} : {} + + /** @type {string} */ + const c = a; +>c : string +>a : number | undefined + + return [b]; +>[b] : string[] +>b : string + + }, + }; +} + +/** @typedef {(b: number, c?: Options) => Promise} Callback */ + +export const cast = /** @type {Callback} */ (async (b, { a } = {}) => { +>cast : Callback +>(async (b, { a } = {}) => { /** @type {string} */ const c = a;}) : Callback +>async (b, { a } = {}) => { /** @type {string} */ const c = a;} : (b: number, { a }?: Options | undefined) => Promise +>b : number +>a : number | undefined +>{} : {} + + /** @type {string} */ + const c = a; +>c : string +>a : number | undefined + +}); + diff --git a/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts b/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts index e0ea93281bfc9..da91e15c341e9 100644 --- a/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts +++ b/tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts @@ -53,3 +53,153 @@ 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; +}; + +export const nonOptional: (input: { a?: string; b?: number }) => void = ({ a, b } = {}) => { + const c: number = a; + const d: string = b; +}; + +declare function takeObject(callback: (input?: { a?: string; b?: number }) => void): void; +takeObject(({ a, b } = {}) => { + const c: number = a; + const d: string = b; +}); + +interface Driver { + rm(path: string, options?: { recursive?: boolean; force?: boolean }): void; +} +export const driver: Driver = { + rm: (path, { recursive, force } = {}) => { + const c: string = recursive; + const d: string = force; + }, +}; + +export const contextualNested: (input?: { inner?: { a?: number } }) => void = ({ inner: { a } = {} } = {}) => { + const c: string = a; +}; + +export function annotatedNestedWithDefault({ inner: { b } = {} }: Outer = {}) { + const c: string = b; +} + +export function annotatedWithDefault({ a, b }: { a?: string; b?: number } = {}) { + const c: number = a; +} + +interface OuterArray { + inner?: number[]; +} + +export function annotatedNestedArray({ inner: [a = 1, b] = [] }: OuterArray) { + const c: string = b; +} + +export const indexSignature: (input?: { [key: string]: number }) => void = ({ a } = {}) => { + const c: string = a; +}; + +export const nestedAny: (input?: any) => void = ({ a: { b } = {} } = {}) => { + b; +}; + +declare function generic(value: T, callback: (input?: Partial) => void): void; +generic({ a: 1 }, ({ a } = {}) => { + const c: string = a; +}); + +export const nonEmptyDefault: (input: { a: boolean; b?: string }) => void = ({ a, b } = { a: false }) => { + const c: string = a; + const d: number = b; +}; + +export const mapped = (input: { a?: number; b?: string }[]) => + input.map(({ a, b } = {}) => { + const c: string = a; + const d: number = b; + }); + +export const mixedDefaults: (args: string[], input?: { a?: string; b?: number }) => void = (args, { a, b = 1 } = {}) => { + const c: number = a; + const d: string = b; +}; + +// @filename: /jsdoc.js +// @ts-check +/** @typedef {(input: { inner?: { a?: number } }) => void} Nested */ + +/** @type {Nested} */ +export function jsdocNested({ inner: { a } = {} }) { + /** @type {string} */ + const c = a; +} + +/** @typedef {{ a?: number }} Options */ +/** @typedef {{ method: (b: string, c?: Options) => Promise }} Container */ + +/** @returns {Container} */ +export function returnsContainer() { + return { + async method(b, { a } = {}) { + /** @type {string} */ + const c = a; + return [b]; + }, + }; +} + +/** @typedef {(b: number, c?: Options) => Promise} Callback */ + +export const cast = /** @type {Callback} */ (async (b, { a } = {}) => { + /** @type {string} */ + const c = a; +}); From 8984402318defbf45a3deac878fdfd154cff02b7 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:17:02 -0800 Subject: [PATCH 2/2] Revert "Align object binding defaults with arrays" This reverts commit 07d1338bac880d9fedc67f37e078f38433ee1b5e (64043), keeping its tests. Padding every missing binding element of a destructured parameter's default made parameters with annotations or contextual types report implicit any for elements that have types, and made nested elements under a contextual type any. Restricting the padding to parameters typed by their initializer fixes those cases, but the remaining benefit, a better parameter type for unchecked JS (59920), is too small to keep carrying a change in this area. Fixes 64431 Fixes 64974 --- tsc/internal/checker/checker.go | 13 +- .../inferredRestTypeFixedOnce.errors.txt | 9 -- ...ngPatternDefaultMissingElements.errors.txt | 134 ++++++------------ ...BindingPatternDefaultMissingElements.types | 46 +++--- 4 files changed, 71 insertions(+), 131 deletions(-) delete mode 100644 tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index eb632669f7326..f5ea5f8e02279 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -17323,12 +17323,11 @@ func (c *Checker) checkDeclarationInitializer(declaration *ast.Node, checkMode C func (c *Checker) padObjectLiteralType(t *Type, pattern *ast.Node) *Type { var missingElements []*ast.Node for _, e := range pattern.Elements() { - if hasDotDotDotToken(e) { - continue - } - name := c.getPropertyNameFromBindingElement(e) - if name != ast.InternalSymbolNameMissing && c.getPropertyOfType(t, name) == nil { - missingElements = append(missingElements, e) + if e.Initializer() != nil { + name := c.getPropertyNameFromBindingElement(e) + if name != ast.InternalSymbolNameMissing && c.getPropertyOfType(t, name) == nil { + missingElements = append(missingElements, e) + } } } if len(missingElements) == 0 { @@ -17340,7 +17339,7 @@ func (c *Checker) padObjectLiteralType(t *Type, pattern *ast.Node) *Type { } for _, e := range missingElements { symbol := c.newSymbol(ast.SymbolFlagsProperty|ast.SymbolFlagsOptional, c.getPropertyNameFromBindingElement(e)) - c.valueSymbolLinks.Get(symbol).resolvedType = c.getTypeFromBindingElement(e, false /*includePatternInType*/, true /*reportErrors*/) + c.valueSymbolLinks.Get(symbol).resolvedType = c.getTypeFromBindingElement(e, false /*includePatternInType*/, false /*reportErrors*/) members.Set(symbol.Name(), symbol) } result := c.newAnonymousType(t.symbol, members, nil, nil, c.getIndexInfosOfType(t)) diff --git a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt b/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt deleted file mode 100644 index d310258fda658..0000000000000 --- a/tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -inferredRestTypeFixedOnce.ts(2,9): error TS7031: Binding element 'cancelable' implicitly has an 'any' type. - - -==== inferredRestTypeFixedOnce.ts (1 errors) ==== - function wrap(_: (...args: Args) => void) {} - 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/objectBindingPatternDefaultMissingElements.errors.txt b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt index b042da9fab1c4..d4b17c79a02d9 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt @@ -1,17 +1,17 @@ -/checked.js(3,5): error TS7031: Binding element 'required' implicitly has an 'any' type. +/checked.js(3,5): error TS2339: Property 'required' does not exist on type '{ optional?: boolean | undefined; }'. /checked.js(8,5): error TS7031: Binding element 'required' implicitly has an 'any' type. -/jsdoc.js(5,40): error TS7031: Binding element 'a' implicitly has an 'any' type. -/jsdoc.js(16,27): error TS7031: Binding element 'a' 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'. /jsdoc.js(18,19): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. -/jsdoc.js(26,58): error TS7031: Binding element 'a' implicitly has an 'any' type. /jsdoc.js(28,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(5,5): error TS2339: Property 'required' does not exist on type '{ optional?: boolean | undefined; }'. /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(30,29): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(30,32): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(15,5): error TS2339: Property 'required' does not exist on type '{}'. +/main.ts(21,19): error TS2353: Object literal may only specify known properties, and 'required' does not exist in type '{ optional?: boolean | undefined; }'. +/main.ts(23,17): error TS2353: Object literal may only specify known properties, and 'required' does not exist in type '{ optional?: boolean | undefined; }'. +/main.ts(25,15): error TS2353: Object literal may only specify known properties, and 'required' does not exist in type '{ optional?: boolean | undefined; }'. /main.ts(33,28): error TS7031: Binding element 'a' implicitly has an 'any' type. /main.ts(33,31): error TS7031: Binding element 'b' implicitly has an 'any' type. /main.ts(35,62): error TS7031: Binding element 'a' implicitly has an 'any' type. @@ -22,63 +22,48 @@ 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(44,62): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(48,65): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(57,51): error TS7031: Binding element 'b' implicitly has an 'any' type. +/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(61,49): error TS7031: Binding element 'b' implicitly has an 'any' type. /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(66,24): error TS7031: Binding element 'summary' implicitly has an 'any' type. -/main.ts(71,12): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(73,65): error TS7031: Binding element 'known' implicitly has an 'any' type. +/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(73,72): error TS7031: Binding element 'unknown' implicitly has an 'any' type. /main.ts(74,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. -/main.ts(77,76): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(77,79): error TS7031: Binding element 'b' implicitly has an 'any' type. /main.ts(78,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. /main.ts(79,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. -/main.ts(83,15): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(83,18): error TS7031: Binding element 'b' implicitly has an 'any' type. /main.ts(84,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. /main.ts(85,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. -/main.ts(92,18): error TS7031: Binding element 'recursive' implicitly has an 'any' type. -/main.ts(92,29): error TS7031: Binding element 'force' implicitly has an 'any' type. /main.ts(93,15): error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. /main.ts(94,15): error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. -/main.ts(98,91): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(102,55): error TS7031: Binding element 'b' implicitly has an 'any' type. +/main.ts(99,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. + Type 'undefined' is not assignable to type 'string'. /main.ts(103,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. /main.ts(107,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. /main.ts(115,11): error TS2322: Type 'number' is not assignable to type 'string'. -/main.ts(118,79): error TS7031: Binding element 'a' implicitly has an 'any' type. /main.ts(119,11): error TS2322: Type 'number' is not assignable to type 'string'. -/main.ts(122,57): error TS7031: Binding element 'b' implicitly has an 'any' type. -/main.ts(127,22): error TS7031: Binding element 'a' implicitly has an 'any' type. /main.ts(128,11): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. -/main.ts(131,83): error TS7031: Binding element 'b' implicitly has an 'any' type. /main.ts(132,11): error TS2322: Type 'boolean' is not assignable to type 'string'. /main.ts(133,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. -/main.ts(137,18): error TS7031: Binding element 'a' implicitly has an 'any' type. -/main.ts(137,21): error TS7031: Binding element 'b' implicitly has an 'any' type. /main.ts(138,15): error TS2322: Type 'number | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. /main.ts(139,15): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. -/main.ts(142,101): error TS7031: Binding element 'a' implicitly has an 'any' type. /main.ts(143,11): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'. /main.ts(144,11): error TS2322: Type 'number' is not assignable to type 'string'. @@ -100,7 +85,7 @@ export const checkedObject = ({ required, ~~~~~~~~ -!!! error TS7031: Binding element 'required' implicitly has an 'any' type. +!!! error TS2339: Property 'required' does not exist on type '{ optional?: boolean | undefined; }'. optional = false, } = {}) => {}; @@ -111,14 +96,14 @@ optional = false, ] = []) => {}; -==== /main.ts (57 errors) ==== +==== /main.ts (39 errors) ==== import { checkedArray, checkedObject } from "./checked"; import { uncheckedArray, uncheckedObject } from "./unchecked"; export const typedObject = ({ required, ~~~~~~~~ -!!! error TS7031: Binding element 'required' implicitly has an 'any' type. +!!! error TS2339: Property 'required' does not exist on type '{ optional?: boolean | undefined; }'. optional = false, } = {}) => {}; @@ -132,26 +117,28 @@ export const typedObjectRest = ({ required, ~~~~~~~~ -!!! error TS7031: Binding element 'required' implicitly has an 'any' type. +!!! error TS2339: Property 'required' does not exist on type '{}'. ...rest } = {}) => { rest; }; uncheckedObject({ required: "value" }); + ~~~~~~~~ +!!! error TS2353: Object literal may only specify known properties, and 'required' does not exist in type '{ optional?: boolean | undefined; }'. uncheckedArray(["value"]); checkedObject({ required: "value" }); + ~~~~~~~~ +!!! error TS2353: Object literal may only specify known properties, and 'required' does not exist in type '{ optional?: boolean | undefined; }'. checkedArray(["value"]); typedObject({ required: "value" }); + ~~~~~~~~ +!!! error TS2353: Object literal may only specify known properties, and 'required' does not exist in type '{ optional?: boolean | undefined; }'. 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); - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. type Arr = (input?: [string?, number?]) => number; export const arr1: Arr = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0); @@ -184,15 +171,17 @@ }); export const arr3: (input?: [{ a?: number }?]) => void = ([{ a } = {}] = []) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. 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 } = {}]] = []) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. 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). @@ -201,8 +190,6 @@ } export function annotatedNested({ inner: { a = 1, b } = {} }: Outer) { - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: string = b; ~ !!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. @@ -210,8 +197,6 @@ } export function annotatedNestedAlone({ inner: { b } = {} }: Outer) { - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: string = b; ~ !!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. @@ -220,23 +205,18 @@ declare const items: { result?: { summary: number } }[]; items.map(({ result: { summary } = {} }) => { - ~~~~~~~ -!!! error TS7031: Binding element 'summary' implicitly has an 'any' type. 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 } = {}) => {}); - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. export const partial: (input?: { known?: number }) => void = ({ known, unknown } = {}) => { - ~~~~~ -!!! error TS7031: Binding element 'known' implicitly has an 'any' type. ~~~~~~~ !!! error TS2339: Property 'unknown' does not exist on type '{ known?: number | undefined; }'. - ~~~~~~~ -!!! error TS7031: Binding element 'unknown' implicitly has an 'any' type. const c: string = known; ~ !!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. @@ -244,10 +224,6 @@ }; export const nonOptional: (input: { a?: string; b?: number }) => void = ({ a, b } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: number = a; ~ !!! error TS2322: Type 'string | undefined' is not assignable to type 'number'. @@ -260,10 +236,6 @@ declare function takeObject(callback: (input?: { a?: string; b?: number }) => void): void; takeObject(({ a, b } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: number = a; ~ !!! error TS2322: Type 'string | undefined' is not assignable to type 'number'. @@ -279,10 +251,6 @@ } export const driver: Driver = { rm: (path, { recursive, force } = {}) => { - ~~~~~~~~~ -!!! error TS7031: Binding element 'recursive' implicitly has an 'any' type. - ~~~~~ -!!! error TS7031: Binding element 'force' implicitly has an 'any' type. const c: string = recursive; ~ !!! error TS2322: Type 'boolean | undefined' is not assignable to type 'string'. @@ -295,14 +263,13 @@ }; export const contextualNested: (input?: { inner?: { a?: number } }) => void = ({ inner: { a } = {} } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. const c: string = a; + ~ +!!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. +!!! error TS2322: Type 'undefined' is not assignable to type 'string'. }; export function annotatedNestedWithDefault({ inner: { b } = {} }: Outer = {}) { - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: string = b; ~ !!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. @@ -327,23 +294,17 @@ } export const indexSignature: (input?: { [key: string]: number }) => void = ({ a } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. const c: string = a; ~ !!! error TS2322: Type 'number' is not assignable to type 'string'. }; export const nestedAny: (input?: any) => void = ({ a: { b } = {} } = {}) => { - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. b; }; declare function generic(value: T, callback: (input?: Partial) => void): void; generic({ a: 1 }, ({ a } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. const c: string = a; ~ !!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. @@ -351,8 +312,6 @@ }); export const nonEmptyDefault: (input: { a: boolean; b?: string }) => void = ({ a, b } = { a: false }) => { - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: string = a; ~ !!! error TS2322: Type 'boolean' is not assignable to type 'string'. @@ -364,10 +323,6 @@ export const mapped = (input: { a?: number; b?: string }[]) => input.map(({ a, b } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. - ~ -!!! error TS7031: Binding element 'b' implicitly has an 'any' type. const c: string = a; ~ !!! error TS2322: Type 'number | undefined' is not assignable to type 'string'. @@ -379,8 +334,6 @@ }); export const mixedDefaults: (args: string[], input?: { a?: string; b?: number }) => void = (args, { a, b = 1 } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. const c: number = a; ~ !!! error TS2322: Type 'string | undefined' is not assignable to type 'number'. @@ -390,16 +343,17 @@ !!! error TS2322: Type 'number' is not assignable to type 'string'. }; -==== /jsdoc.js (5 errors) ==== +==== /jsdoc.js (3 errors) ==== // @ts-check /** @typedef {(input: { inner?: { a?: number } }) => void} Nested */ /** @type {Nested} */ export function jsdocNested({ inner: { a } = {} }) { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. /** @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'. } /** @typedef {{ a?: number }} Options */ @@ -409,8 +363,6 @@ export function returnsContainer() { return { async method(b, { a } = {}) { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. /** @type {string} */ const c = a; ~ @@ -424,8 +376,6 @@ /** @typedef {(b: number, c?: Options) => Promise} Callback */ export const cast = /** @type {Callback} */ (async (b, { a } = {}) => { - ~ -!!! error TS7031: Binding element 'a' implicitly has an 'any' type. /** @type {string} */ const c = a; ~ diff --git a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types index dc5b6a3758eda..7157219b2db4e 100644 --- a/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types +++ b/tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types @@ -2,8 +2,8 @@ === /unchecked.js === export const uncheckedObject = ({ ->uncheckedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void ->({ required, optional = false,} = {}) => {} : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>uncheckedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void +>({ required, optional = false,} = {}) => {} : ({ required, optional, }?: { optional?: boolean | undefined; }) => void required, >required : any @@ -32,8 +32,8 @@ export const uncheckedArray = ([ === /checked.js === // @ts-check export const checkedObject = ({ ->checkedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void ->({ required, optional = false,} = {}) => {} : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>checkedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void +>({ required, optional = false,} = {}) => {} : ({ required, optional, }?: { optional?: boolean | undefined; }) => void required, >required : any @@ -62,15 +62,15 @@ export const checkedArray = ([ === /main.ts === import { checkedArray, checkedObject } from "./checked"; >checkedArray : ([required, optional,]?: [any?, (boolean | undefined)?]) => void ->checkedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>checkedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void import { uncheckedArray, uncheckedObject } from "./unchecked"; >uncheckedArray : ([required, optional,]?: [any?, (boolean | undefined)?]) => void ->uncheckedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>uncheckedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void export const typedObject = ({ ->typedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void ->({ required, optional = false,} = {}) => {} : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>typedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void +>({ required, optional = false,} = {}) => {} : ({ required, optional, }?: { optional?: boolean | undefined; }) => void required, >required : any @@ -97,8 +97,8 @@ export const typedArray = ([ >[] : [] export const typedObjectRest = ({ ->typedObjectRest : ({ required, ...rest }?: { required?: any; }) => void ->({ required, ...rest} = {}) => { rest;} : ({ required, ...rest }?: { required?: any; }) => void +>typedObjectRest : ({ required, ...rest }?: {}) => void +>({ required, ...rest} = {}) => { rest;} : ({ required, ...rest }?: {}) => void required, >required : any @@ -116,7 +116,7 @@ export const typedObjectRest = ({ uncheckedObject({ required: "value" }); >uncheckedObject({ required: "value" }) : void ->uncheckedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>uncheckedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void >{ required: "value" } : { required: string; } >required : string >"value" : "value" @@ -129,7 +129,7 @@ uncheckedArray(["value"]); checkedObject({ required: "value" }); >checkedObject({ required: "value" }) : void ->checkedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>checkedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void >{ required: "value" } : { required: string; } >required : string >"value" : "value" @@ -142,7 +142,7 @@ checkedArray(["value"]); typedObject({ required: "value" }); >typedObject({ required: "value" }) : void ->typedObject : ({ required, optional, }?: { optional?: boolean | undefined; required?: any; }) => void +>typedObject : ({ required, optional, }?: { optional?: boolean | undefined; }) => void >{ required: "value" } : { required: string; } >required : string >"value" : "value" @@ -246,13 +246,13 @@ export const arr3: (input?: [{ a?: number }?]) => void = ([{ a } = {}] = []) => >input : [({ a?: number; } | undefined)?] | undefined >a : number | undefined >([{ a } = {}] = []) => { const b: string = a;} : ([{ a }]?: [({ a?: number; } | undefined)?] | undefined) => void ->a : any +>a : number | undefined >{} : {} >[] : [] const b: string = a; >b : string ->a : any +>a : number | undefined }; @@ -261,13 +261,13 @@ export const arr4: (input?: { a?: number }[]) => void = ([...[{ a } = {}]] = []) >input : { a?: number; }[] | undefined >a : number | undefined >([...[{ a } = {}]] = []) => { const b: string = a;} : ([...[{ a }]]?: { a?: number; }[] | undefined) => void ->a : any +>a : number | undefined >{} : {} >[] : never[] const b: string = a; >b : string ->a : any +>a : number | undefined }; @@ -315,12 +315,12 @@ items.map(({ result: { summary } = {} }) => { >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 : any +>summary : number | undefined >{} : {} const c: string = summary; >c : string ->summary : any +>summary : number | undefined }); @@ -434,13 +434,13 @@ export const contextualNested: (input?: { inner?: { a?: number } }) => void = ({ >a : number | undefined >({ inner: { a } = {} } = {}) => { const c: string = a;} : ({ inner: { a } }?: { inner?: { a?: number; }; } | undefined) => void >inner : any ->a : any +>a : number | undefined >{} : {} >{} : {} const c: string = a; >c : string ->a : any +>a : number | undefined }; @@ -617,13 +617,13 @@ export const mixedDefaults: (args: string[], input?: { a?: string; b?: number }) export function jsdocNested({ inner: { a } = {} }) { >jsdocNested : (input: { inner?: { a?: number; }; }) => void >inner : any ->a : any +>a : number | undefined >{} : {} /** @type {string} */ const c = a; >c : string ->a : any +>a : number | undefined } /** @typedef {{ a?: number }} Options */