Skip to content

Anonymous Function Type Not Constraining Object Return Value #59586

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
atpur-rafif opened this issue Aug 10, 2024 · 4 comments
Closed

Anonymous Function Type Not Constraining Object Return Value #59586

atpur-rafif opened this issue Aug 10, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@atpur-rafif
Copy link

atpur-rafif commented Aug 10, 2024

πŸ”Ž Search Terms

anonymous function type, return object value

πŸ•— Version & Regression Information

Same behavior for version 3.9.7, 4.9.5, 5.5.4, 5.6.0-beta and v5.6.0-dev.20240810.

⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0&ts=5.5.4#code/C4TwDgpgBAShwFcBOA7AagQwDYOgXigG8okAuKFBAWwCMIkoBfAKFEigAUMkMrMd8RKGHKVa9Js2YAzBCgDGwAJYB7FFGkAKMN17kuPPtlwBKcnESp+uIswCQSeMnWF7D8jsMA6MABo3APQBUAACwADOALQQAB6QitFISCpIbvTJZFAAROkpWf52jADczCxarlCVwuQADAVBoRHRcRAJualVUO3kOUl5pSZSbNAAggBiUATaulT6M9YQJpMAfLBOVsYQJfJq4cBQGNLk45NQ04akBrwLS3irrg7rLm6Znrw+BXYNYVGx8cCJDJpPqZXoZfL2YqlEqHTQVKoiKB1ezfJp-VoA9rMTrdbLtLIDIA

πŸ’» Code

type ReturnValue = { r: number }
type ParamValue = { p: number }

function f(param: ParamValue): ReturnValue {
	return {
		r: param.p,
		// @ts-expect-error
		error: "error",
	};
}
f({
    p: 0,
	// @ts-expect-error
    error: "error"
})

type AF = (param: ParamValue) => ReturnValue;
const af: AF = (param:ParamValue) => {
	return {
		r: param.p,
		// @ts-expect-error
		error: "error",
	};
};
af({
    p: 0,
	// @ts-expect-error
    error: "error"
})

πŸ™ Actual behavior

At third // @ts-expect-error (return object of function af) giving error because it's not used. Since there is no error at that line.

πŸ™‚ Expected behavior

Typescript should give error about extraneous error property, since for non-anonymous type function f gave that error. This same error happen for parameter of both af and f function. I assume this should be the case for return value too.

@MartinJohns
Copy link
Contributor

This is working as intended. Your function lacks a return type annotation, so the return type is inferred. The inferred type is then checked for assignability, where excess property checks don't happen anymore.

There are many issues about this.

@jcalz
Copy link
Contributor

jcalz commented Aug 10, 2024

Duplicate #241 and others

@atpur-rafif
Copy link
Author

This is working as intended. Your function lacks a return type annotation, so the return type is inferred. The inferred type is then checked for assignability, where excess property checks don't happen anymore.

There are many issues about this.

I tried to remove annotation for parameter at anonymous function af. So, if it's the case (about lack of annotation), then at last // @ts-expect-error (when calling function af), it should not throwing any error, because the parameter is inferred. But it still throwing the error.

type AF = (param: ParamValue) => ReturnValue;
const af: AF = (param) => {
	return {
		r: param.p,
		// @ts-expect-error
		error: "error",
	};
};
af({
    p: 0,
	// @ts-expect-error
    error: "error"
})

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 12, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants