Skip to content

Calling never-returning method on a subtype of function does not consider following code as unreachableΒ #60059

Closed as not planned
@AlCalzone

Description

@AlCalzone

πŸ”Ž Search Terms

function member never ts2534

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about never

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20240925#code/C4TwDgpgBAaghgGwJYBM7CQewHYDECu2AxhjlALxQAUAdHXAM4MQBOp2DAXFIQNbaYA7tgDaAXQCUFAHxQAbplQBuAFAqk2YKwBmcItHjI0WqBAAeW7CgaxEqdFjyESjqAG8VASF1IEVCdzYEHKsqgC+atrO7PJ2xhAAkpqs2Ii09Eys7Fw82PxCopLcCqjuXkja1ACEjMxsjgw0wawgVABCmJgIEHDYElIenp7AABYsQlBBglAAoizjLFQARHJxDmQ+3ShLEqqeEREqRDgMwLFG6NCUqxdaSVosqQg0AEYaKFSEKBDaGhAoUkYtluEFUN3sWhomwo1Ck5Fk4PiVF0CGYgJsfAEwigQKCIRYqki0Vcm38gWaLDKUHOEIgULgvn8qmpAHoWdSORyAHoAfhUhyixBiQVO-1wDL8AUmFLKnjZk0wpnmmEpI1YEC8pN2XnlvP5ahFWhQ4sZuyg8tGSBso3Gghs7wg-xUCEwAHNlihSgIzjahDtVEA

πŸ’» Code

type ValidationFunction = (...assertions: unknown[]) => void;

interface Validate extends ValidationFunction {
	fail(): never;
}

function validateInternal(...assertions: unknown[]): void {
	if (!assertions.every(Boolean)) {
		throw new Error("validation failed");
	}
}

const validate = validateInternal.bind(undefined) as Validate;
validate.fail = () => validate(false) as unknown as never;


function fail(): never {
  validate.fail();
  //        ^? (method) Validate.fail(): never
}

function nestedFail(): never {
	// no error here
	fail();
	// ^? function fail(): never
}

nestedFail(); // this throws indeed
log("did not throw");

πŸ™ Actual behavior

TS complains about the never return type of function fail():

A function returning 'never' cannot have a reachable end point.(2534)

πŸ™‚ Expected behavior

No errors

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions