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

Array.map typechecking issues, doesn't typecheck what the .map callback returns #31425

Closed
benoitgrelard opened this issue May 16, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@benoitgrelard
Copy link

benoitgrelard commented May 16, 2019

TypeScript Version: reproducible in at least 3.3.3333 and above

Search Terms: Array map generic extra property

Code

type Token = {
    value: string
}

const things = ['one', 'two'];

// using .map generic — doesn't fail typechecking
function getTokens1() {
    return things.map<Token>(thing => ({
        value: thing,
        // should say `somethingElse` does not exist on type `Token`
        somethingElse: true
    }));
}

// using return type — doesn't fail typechecking
function getTokens2(): Token[] {
    const tokens = things.map(thing => ({
        value: thing,
        // should say `somethingElse` does not exist on type `Token`
        somethingElse: true
    }));

    // type of tokens is reported as `{ value: string; somethingElse: boolean; }[]`
    // so how could it be compatible with `Token[]`?
    return tokens;
}


// following use cases as working proofs and potential workarounds

// fails typechecking correctly
const token: Token = {
    value: 'one',
    somethingElse: true // `somethingElse` does not exist on type `Token`
};

// fails typechecking correctly — potential workaround
function getTokens3() {
    // note the `Token` inline return type
    return things.map((thing): Token => ({
        value: thing,
        somethingElse: true // `somethingElse` does not exist on type `Token`
    }));
}

Expected behavior:

I would expect in getTokens1 using the .map<U> generic form that it would typecheck what .map returns. Similarly, I would expect in getTokens2 that it would complain as I am precising the return type of the function and the inference itself seems to show an incompatible type.

Actual behavior:

It doesn't seem to typecheck properly when using Array.map.

Playground Link: See above example in playground

Related Issues:

@fatcerberus
Copy link

fatcerberus commented May 16, 2019

I’m not sure why the first case isn’t an error (it seems like it should be, you’ve explicitly provided the result type for map and the object literal is still fresh when returned), but the second case is expected. See my comment here re: structural typing: #31412 (comment)

Also see - https://github.com/basarat/typescript-book/blob/master/docs/types/freshness.md#freshness

@RyanCavanaugh
Copy link
Member

Duplicate #241

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 16, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a '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

4 participants