Skip to content
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

In JS, return type of type reference in @type tag is ignored on function declarations. #25525

Closed
ghost opened this issue Jul 9, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue

Comments

@ghost
Copy link

ghost commented Jul 9, 2018

TypeScript Version: 3.0.0-dev.20180707

Code

/** @typedef {"a" | "b"} U */

/** @return {U} */
function f0() { return "a"; }

/** @type {() => U} */
function f1() { return "a"; }

/** @typedef {() => U} RetU */

/** @type {RetU} */
function f2() { return "a"; }

/** @type {RetU} */
const f3 = () => "a";

const x0 = f0();
const x1 = f1();
const x2 = f2();
const x3 = f3();

Expected behavior:

x0 through x3 are all of type "a" | "b".

Actual behavior:

x2 is of type string.

Noticed while reviewing #25486.

@ghost ghost assigned sandersn Jul 9, 2018
@ghost ghost added Bug A bug in TypeScript Salsa Domain: JSDoc Relates to JSDoc parsing and type generation checkJs Relates to checking JavaScript using TypeScript labels Jul 9, 2018
@mhegazy mhegazy added this to the TypeScript 3.1 milestone Jul 9, 2018
@sandersn sandersn changed the title No contextual type from function type behind an alias In JS, return type of type reference in @type tag is ignored on function declarations. Jul 10, 2018
@sandersn
Copy link
Member

Technically, these aren't contextual types, but type annotations. That is the reason they don't work, actually: Typescript has no whole-type annotations for declarations so there was no easy analogue to translate the JSDoc to. Here's a smaller repro, which should not error, but currently does:

/** @typedef {(x: 'hi' | 'bye') => 0 | 1 | 2} Argle */
/** @type {Argle} */
function f(s) {
    return 0;
}

/** @type {0 | 1 | 2} */
var zeroonetwo = f('hi')

sandersn added a commit that referenced this issue Jul 11, 2018
This only happens in the checker, where the type is easily accessible.
The syntax-based check in getEffectiveReturnTypeNode as a fast path, and
for other uses that don't want to make a call to getTypeFromTypeNode.

Fixes #25525
@sandersn sandersn added the Fixed A PR has been merged for this issue label Jul 11, 2018
sandersn added a commit that referenced this issue Jul 12, 2018
* Get return type from `@type` tag

This only happens in the checker, where the type is easily accessible.
The syntax-based check in getEffectiveReturnTypeNode as a fast path, and
for other uses that don't want to make a call to getTypeFromTypeNode.

Fixes #25525

* Implement PR suggestions

* Error when type tag isn't callable

* Fix lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants