Skip to content

Issue with "as" keyword when target type uses union types for properties #6919

@massimocode

Description

@massimocode

From what I understand, the as keyword allows you to omit properties... but it doesn't seem to work when union types are involved...

interface Foo {
  foo: string;
  bar: Bar;
  baz: Baz;
  union: Bar | Baz;
}
interface Bar {
  bar: string;
}
interface Baz {
  baz: string;
}

let this_works = {
  foo: 'hello'
} as Foo;

let this_also_works1 = {
  bar: { bar: 'hello' }
} as Foo;

let this_also_works2 = {
  baz: { baz: 'hello' }
} as Foo;

let union_cant_be_assigned_unless_all_other_properties_assigned = {
  union: { bar: 'hello' }
} as Foo;

I've tested on TypeScript 1.7.5 (current release) and typescript@next (Version 1.9.0-dev.20160205)

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions