Skip to content

Re-add undefined assignability check #53561

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35664,8 +35664,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const functionFlags = getFunctionFlags(func);
const type = returnType && unwrapReturnType(returnType, functionFlags);

// Functions with an explicitly specified 'undefined, 'void', 'any' or 'unknown' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Undefined | TypeFlags.Void | TypeFlags.Any | TypeFlags.Unknown)) {
// Functions with an explicitly specified 'undefined, 'void' or 'any' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Undefined | TypeFlags.Void | TypeFlags.Any)) {
return;
}

Expand All @@ -35691,7 +35691,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
}
}
else if (type && strictNullChecks) {
else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
}
else if (compilerOptions.noImplicitReturns) {
Expand Down
5 changes: 4 additions & 1 deletion tests/baselines/reference/unknownType1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ tests/cases/conformance/types/unknown/unknownType1.ts(128,5): error TS2322: Type
tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type 'number' is not assignable to type '{ [x: string]: unknown; }'.
tests/cases/conformance/types/unknown/unknownType1.ts(143,29): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(144,29): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/conformance/types/unknown/unknownType1.ts(156,14): error TS2700: Rest types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(162,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/types/unknown/unknownType1.ts(170,9): error TS2322: Type 'T' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(171,9): error TS2322: Type 'U' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type 'T' is not assignable to type '{}'.


==== tests/cases/conformance/types/unknown/unknownType1.ts (26 errors) ====
==== tests/cases/conformance/types/unknown/unknownType1.ts (27 errors) ====
// In an intersection everything absorbs unknown

type T00 = unknown & null; // null
Expand Down Expand Up @@ -221,6 +222,8 @@ tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type
// Functions with unknown return type don't need return expressions

function f27(): unknown {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be sure: This error should be emitted because noImplicitReturns is true by default right?

Copy link
Member

Choose a reason for hiding this comment

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

Actually no, this error is issued regardless of noImplicitReturns. It occurs when there are no return statements at all in the function body (i.e. when hasExplicitReturn is false).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wait then this comment is incorrect.

Copy link
Member

Choose a reason for hiding this comment

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

I'm so confused. Can we get a playground link of what was desirable in TS 5.0, but is now incorrect in nightly?

~~~~~~~
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
}

// Rest type cannot be created from unknown
Expand Down