Skip to content

TS2345 Error when using string literal types in array.map #11054

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
k8w opened this issue Sep 22, 2016 · 2 comments
Closed

TS2345 Error when using string literal types in array.map #11054

k8w opened this issue Sep 22, 2016 · 2 comments

Comments

@k8w
Copy link

k8w commented Sep 22, 2016

TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx) 2.0.2@rc

Code

interface FolderContentItem{
    type: 'folder' | 'file';
}

let a:FolderContentItem[] = [];
a = [1,2,3,4,5].map(v=>({type:'folder'}))

Expected behavior:
Compiled successfully

Actual behavior:

test.ts(6,1): error TS2322: Type '{ type: string; }[]' is not assignable to type 'FolderContentItem[]'.
  Type '{ type: string; }' is not assignable to type 'FolderContentItem'.
    Types of property 'type' are incompatible.
      Type 'string' is not assignable to type '"folder" | "file"'.
@nippur72
Copy link

It doesn't depend specifically on map, it's because the constant {type:'folder'} is misinterpreted as {type:string}:

interface FolderContentItem{
    type: "folder";
}

function foo() {  // return type is { type: string }
   return {type:'folder'};
}

let a:FolderContentItem = foo();  // error

@mhegazy
Copy link
Contributor

mhegazy commented Sep 26, 2016

The issue here is the type of a is never used. i have filed a new issue to track this change: #11152

@mhegazy mhegazy closed this as completed Sep 26, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants