microsoft / microsoft/TypeScript

Any type is inferred when function input parameter is set to some value

Open
#37,100 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference Needs Human Review Needs More Info
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.8.2

Search Terms: any type is inferred

Code

interface ContinuationQueryResult<T> {
    documents: T[];
    continuation?: string;
}

function abc(continuation?: string): Promise<ContinuationQueryResult<string>> {
    return <any>undefined;
}

async function test() {
    // When not explicitly set to undefined type is inferred correctly.
    let continuation: string | undefined = undefined;

    do {
        const summaries = await abc(continuation);

        continuation = summaries.continuation;
    } while (continuation)
}

Expected behavior:
Type of summaries variable should be ContinuationQueryResult<string>.

Actual behavior:
summaries variable is inferred as any.

Playground Link: https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgMIHtygK5zMTARW2gE8AlCAZ2wBswAeAFQD5kBvAWAChk-kAJugTYAthHBUAXMiYBtALoBuHv2QJM+ELnyYA-DKpgooAOYruAXx48Y2EAl0hkcAEYIAFBqza8BEAbIRiYgpgCUMgAKUOiiwFQQDBg+Ov7EZJQ09AzBZixsXLz8UBBg2FDODHAgpCz2AhAwoBACFtbcPHBUpA7Idg5OyJBGHmEcqvwA9JPIAOoAFhLIIOhgyBAAHgAOtMAIwGC0pEGlQ+jI9Y3NAkOkWyjxyKAw0CU3GlAljkcAdBN8tFO3i0qUwhmMZmQAB8LiAGk0QC1kABeWHw64Wf6Cc6FNRqbxGIJiURwEzUFEuADucAOLncXk0OD8mDCmKKePUjN8g1RNFEJLJVB+wKZTgsaksyEp82AgOQDJSzJAYR4liAA

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and the test function, comparing inference when continuation is initialized to undefined versus another value. Trace the type-checking path for the optional argument and add a regression test; done means summaries is inferred as ContinuationQueryResult rather than any.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.