Skip to content

Short-circuit or (||) allows incorrect code to typecheck #13756

@marcomorain

Description

@marcomorain

TypeScript Version: 2.1.4

Code

interface Data {
  items: number[]
}

export interface DataCollection {
  [i: string]: Data
}

const collection: DataCollection = {}

const datum: Data = collection['not-present'] || 'horse' // this line should  fail the type-check

datum.items = [1, 2, 3];

Expected behavior:

A compile time error.

I would expect something like:

error TS2322: Type '"horse"' is not assignable to type 'Data'.

Actual behavior:

A runtime error:

Uncaught TypeError: Cannot create property 'items' on string 'horse'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions