Skip to content

Fix contextually typed parameter issues #36476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 71 additions & 82 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts(8,29): error TS7031: Binding element 'foo' implicitly has an 'any' type.
tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts(14,27): error TS7006: Parameter 'foo' implicitly has an 'any' type.
tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts(27,42): error TS7031: Binding element 'foo' implicitly has an 'any' type.
tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts(24,24): error TS7006: Parameter 'x' implicitly has an 'any' type.
tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts(40,5): error TS7006: Parameter 'x' implicitly has an 'any' type.


==== tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts (3 errors) ====
==== tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts (2 errors) ====
declare function id1<T>(input: T): T;
declare function id2<T extends (x: any) => any>(input: T): T;
declare function id3<T extends (x: { foo: any }) => any>(input: T): T;
declare function id4<T extends (x: { foo?: number }) => any>(input: T): T;
declare function id5<T extends (x?: number) => any>(input: T): T;

const f10 = function ({ foo = 42 }) { return foo };
const f11 = id1(function ({ foo = 42 }) { return foo }); // Implicit any error
~~~
!!! error TS7031: Binding element 'foo' implicitly has an 'any' type.
const f11 = id1(function ({ foo = 42 }) { return foo });
const f12 = id2(function ({ foo = 42 }) { return foo });
const f13 = id3(function ({ foo = 42 }) { return foo });
const f14 = id4(function ({ foo = 42 }) { return foo });

const f20 = function (foo = 42) { return foo };
const f21 = id1(function (foo = 42) { return foo }); // Implicit any error
~~~~~~~~
!!! error TS7006: Parameter 'foo' implicitly has an 'any' type.
const f21 = id1(function (foo = 42) { return foo });
const f22 = id2(function (foo = 42) { return foo });
const f25 = id5(function (foo = 42) { return foo });

const f1 = (x = 1) => 0; // number
const f2: any = (x = 1) => 0; // number
const f3: unknown = (x = 1) => 0; // number
const f4: Function = (x = 1) => 0; // number
const f5: (...args: any[]) => any = (x = 1) => 0; // any
const f6: () => any = (x = 1) => 0; // number
const f7: () => any = (x?) => 0; // Implicit any error
~~
!!! error TS7006: Parameter 'x' implicitly has an 'any' type.
const f8: () => any = (...x) => 0; // []

declare function g1<T>(x: T): T;
declare function g2<T extends any>(x: T): T;
declare function g3<T extends unknown>(x: T): T;
declare function g4<T extends Function>(x: T): T;
declare function g5<T extends (...args: any[]) => any>(x: T): T;
declare function g6<T extends () => any>(x: T): T;

g1((x = 1) => 0); // number
g2((x = 1) => 0); // number
g3((x = 1) => 0); // number
g4((x = 1) => 0); // number
g5((x = 1) => 0); // any
g6((x = 1) => 0); // number
g6((x?) => 0); // Implicit any error
~~
!!! error TS7006: Parameter 'x' implicitly has an 'any' type.
g6((...x) => 0); // []

// Repro from #28816

function id<T>(input: T): T { return input }
Expand All @@ -35,8 +59,35 @@ tests/cases/compiler/contextuallyTypedParametersWithInitializers.ts(27,42): erro

const newGetFoo = id(getFoo);
const newGetFoo2 = id(function getFoo ({ foo = 42 }) {
~~~
!!! error TS7031: Binding element 'foo' implicitly has an 'any' type.
return foo;
});

// Repro from comment in #30840

declare function memoize<F extends Function>(func: F): F;

function add(x: number, y = 0): number {
return x + y;
}
const memoizedAdd = memoize(add);

const add2 = (x: number, y = 0): number => x + y;
const memoizedAdd2 = memoize(add2);

const memoizedAdd3 = memoize((x: number, y = 0): number => x + y);

// Repro from #36052

declare function execute(script: string | Function): Promise<string>;

export function executeSomething() {
return execute((root: HTMLElement, debug = true) => {
if (debug) {
root.innerHTML = '';
}
});
}

const fz1 = (debug = true) => false;
const fz2: Function = (debug = true) => false;

189 changes: 155 additions & 34 deletions tests/baselines/reference/contextuallyTypedParametersWithInitializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,41 @@ declare function id4<T extends (x: { foo?: number }) => any>(input: T): T;
declare function id5<T extends (x?: number) => any>(input: T): T;

const f10 = function ({ foo = 42 }) { return foo };
const f11 = id1(function ({ foo = 42 }) { return foo }); // Implicit any error
const f11 = id1(function ({ foo = 42 }) { return foo });
const f12 = id2(function ({ foo = 42 }) { return foo });
const f13 = id3(function ({ foo = 42 }) { return foo });
const f14 = id4(function ({ foo = 42 }) { return foo });

const f20 = function (foo = 42) { return foo };
const f21 = id1(function (foo = 42) { return foo }); // Implicit any error
const f21 = id1(function (foo = 42) { return foo });
const f22 = id2(function (foo = 42) { return foo });
const f25 = id5(function (foo = 42) { return foo });

const f1 = (x = 1) => 0; // number
const f2: any = (x = 1) => 0; // number
const f3: unknown = (x = 1) => 0; // number
const f4: Function = (x = 1) => 0; // number
const f5: (...args: any[]) => any = (x = 1) => 0; // any
const f6: () => any = (x = 1) => 0; // number
const f7: () => any = (x?) => 0; // Implicit any error
const f8: () => any = (...x) => 0; // []

declare function g1<T>(x: T): T;
declare function g2<T extends any>(x: T): T;
declare function g3<T extends unknown>(x: T): T;
declare function g4<T extends Function>(x: T): T;
declare function g5<T extends (...args: any[]) => any>(x: T): T;
declare function g6<T extends () => any>(x: T): T;

g1((x = 1) => 0); // number
g2((x = 1) => 0); // number
g3((x = 1) => 0); // number
g4((x = 1) => 0); // number
g5((x = 1) => 0); // any
g6((x = 1) => 0); // number
g6((x?) => 0); // Implicit any error
g6((...x) => 0); // []

// Repro from #28816

function id<T>(input: T): T { return input }
Expand All @@ -28,18 +53,48 @@ const newGetFoo = id(getFoo);
const newGetFoo2 = id(function getFoo ({ foo = 42 }) {
return foo;
});

// Repro from comment in #30840

declare function memoize<F extends Function>(func: F): F;

function add(x: number, y = 0): number {
return x + y;
}
const memoizedAdd = memoize(add);

const add2 = (x: number, y = 0): number => x + y;
const memoizedAdd2 = memoize(add2);

const memoizedAdd3 = memoize((x: number, y = 0): number => x + y);

// Repro from #36052

declare function execute(script: string | Function): Promise<string>;

export function executeSomething() {
return execute((root: HTMLElement, debug = true) => {
if (debug) {
root.innerHTML = '';
}
});
}

const fz1 = (debug = true) => false;
const fz2: Function = (debug = true) => false;


//// [contextuallyTypedParametersWithInitializers.js]
"use strict";
exports.__esModule = true;
var f10 = function (_a) {
var _b = _a.foo, foo = _b === void 0 ? 42 : _b;
return foo;
};
var f11 = id1(function (_a) {
var _b = _a.foo, foo = _b === void 0 ? 42 : _b;
return foo;
}); // Implicit any error
});
var f12 = id2(function (_a) {
var _b = _a.foo, foo = _b === void 0 ? 42 : _b;
return foo;
Expand All @@ -59,7 +114,7 @@ var f20 = function (foo) {
var f21 = id1(function (foo) {
if (foo === void 0) { foo = 42; }
return foo;
}); // Implicit any error
});
var f22 = id2(function (foo) {
if (foo === void 0) { foo = 42; }
return foo;
Expand All @@ -68,6 +123,70 @@ var f25 = id5(function (foo) {
if (foo === void 0) { foo = 42; }
return foo;
});
var f1 = function (x) {
if (x === void 0) { x = 1; }
return 0;
}; // number
var f2 = function (x) {
if (x === void 0) { x = 1; }
return 0;
}; // number
var f3 = function (x) {
if (x === void 0) { x = 1; }
return 0;
}; // number
var f4 = function (x) {
if (x === void 0) { x = 1; }
return 0;
}; // number
var f5 = function (x) {
if (x === void 0) { x = 1; }
return 0;
}; // any
var f6 = function (x) {
if (x === void 0) { x = 1; }
return 0;
}; // number
var f7 = function (x) { return 0; }; // Implicit any error
var f8 = function () {
var x = [];
for (var _i = 0; _i < arguments.length; _i++) {
x[_i] = arguments[_i];
}
return 0;
}; // []
g1(function (x) {
if (x === void 0) { x = 1; }
return 0;
}); // number
g2(function (x) {
if (x === void 0) { x = 1; }
return 0;
}); // number
g3(function (x) {
if (x === void 0) { x = 1; }
return 0;
}); // number
g4(function (x) {
if (x === void 0) { x = 1; }
return 0;
}); // number
g5(function (x) {
if (x === void 0) { x = 1; }
return 0;
}); // any
g6(function (x) {
if (x === void 0) { x = 1; }
return 0;
}); // number
g6(function (x) { return 0; }); // Implicit any error
g6(function () {
var x = [];
for (var _i = 0; _i < arguments.length; _i++) {
x[_i] = arguments[_i];
}
return 0;
}); // []
// Repro from #28816
function id(input) { return input; }
function getFoo(_a) {
Expand All @@ -79,36 +198,38 @@ var newGetFoo2 = id(function getFoo(_a) {
var _b = _a.foo, foo = _b === void 0 ? 42 : _b;
return foo;
});
function add(x, y) {
if (y === void 0) { y = 0; }
return x + y;
}
var memoizedAdd = memoize(add);
var add2 = function (x, y) {
if (y === void 0) { y = 0; }
return x + y;
};
var memoizedAdd2 = memoize(add2);
var memoizedAdd3 = memoize(function (x, y) {
if (y === void 0) { y = 0; }
return x + y;
});
function executeSomething() {
return execute(function (root, debug) {
if (debug === void 0) { debug = true; }
if (debug) {
root.innerHTML = '';
}
});
}
exports.executeSomething = executeSomething;
var fz1 = function (debug) {
if (debug === void 0) { debug = true; }
return false;
};
var fz2 = function (debug) {
if (debug === void 0) { debug = true; }
return false;
};


//// [contextuallyTypedParametersWithInitializers.d.ts]
declare function id1<T>(input: T): T;
declare function id2<T extends (x: any) => any>(input: T): T;
declare function id3<T extends (x: {
foo: any;
}) => any>(input: T): T;
declare function id4<T extends (x: {
foo?: number;
}) => any>(input: T): T;
declare function id5<T extends (x?: number) => any>(input: T): T;
declare const f10: ({ foo }: {
foo?: number | undefined;
}) => number;
declare const f11: ({ foo }: any) => any;
declare const f12: ({ foo }: any) => any;
declare const f13: ({ foo }: {
foo: any;
}) => any;
declare const f14: ({ foo }: {
foo?: number | undefined;
}) => number;
declare const f20: (foo?: number) => number;
declare const f21: (foo?: any) => any;
declare const f22: (foo?: any) => any;
declare const f25: (foo?: number | undefined) => number;
declare function id<T>(input: T): T;
declare function getFoo({ foo }: {
foo?: number | undefined;
}): number;
declare const newGetFoo: typeof getFoo;
declare const newGetFoo2: ({ foo }: any) => any;
export declare function executeSomething(): Promise<string>;
Loading