镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@k8w

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"'.

Activity

  1. nippur72 commented on Sep 22, 2016

    @nippur72

    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
  2. mhegazy commented on Sep 26, 2016

    @mhegazy
    Contributor

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

  3. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions