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
Add an extra test case related to return statements and annotated return types #52628
Add an extra test case related to return statements and annotated return types #52628
Conversation
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
I noticed this in the test, but I'm honestly a little suspicious of this code because the type variable is only observed once, which makes me think that it's already bound to be broke as it is and maybe should be changed. But, I think that PR had other things going on too. |
Yea, you might be right here - this only works within the context of the contextual type and allowing this is kinda suspicious: type PropOfRaw<T> = readonly T[] | "not-array" | "no-prop";
declare function isString(text: unknown): text is string;
declare function getPropFromRaw<T>(
prop: "files" | "include" | "exclude" | "references",
validateElement: (value: unknown) => boolean,
elementTypeName: string
): PropOfRaw<T>;
function getSpecsFromRaw(
prop: "files" | "include" | "exclude"
): PropOfRaw<string> {
const val = getPropFromRaw(prop, isString, "string");
return val; // error
} This code errors even though I only extracted the returned value to the local |
tests/cases/compiler/contextualTypingReturnStatementWithReturnTypeAnnotation.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning back to this, I guess it's okay to take this case, it's just a little weird because the type var is only observed once. But there's clearly no question as to which one it is for this example.
I tried to figure out if we could change this code but I can't quite get it right; the code secretly lies about the type it returns and is totally unsafe.
Hmm, I took another look at this as I totally forgot about this PR... this code is so... suspicious 😅 Like, it was broken by one of my PRs, and thus might be worthwhile to have it as a test case. I still wonder though if this should actually be permitted. I can't find a reasonable explanation for this: type PropOfRaw<T> = readonly T[] | "not-array" | "no-prop";
declare function isString(text: unknown): text is string;
declare function getPropFromRaw<T>(
prop: "files" | "include" | "exclude" | "references",
validateElement: (value: unknown) => boolean,
elementTypeName: string
): PropOfRaw<T>;
function getSpecsFromRaw1(
prop: "files" | "include" | "exclude"
): PropOfRaw<string> {
const val = getPropFromRaw(prop, isString, "string");
return val; // error
}
function getSpecsFromRaw2(
prop: "files" | "include" | "exclude"
): PropOfRaw<string> {
return getPropFromRaw(prop, isString, "string"); // ok?
} Dunno, perhaps I will open a new issue to continue discussing this 🤷♂️ |
After second thought... maybe this is just valid "transitively"? While this code is suspicious on its own... we can't write the implementation for |
This is a test case extracted from the "self check". I managed to break it in #52622 without breaking the test suite so I figured out that it's best to get it into the test suite.