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

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

Closed
massimocode opened this issue Feb 5, 2016 · 2 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@massimocode
Copy link

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)

@ahejlsberg
Copy link
Member

This is a known issue. We have a fix in #5517 but we haven't decided if we really want to introduce a third kind of type relation.

In last example the issue is that neither { union: { bar: string } } nor Foo is assignable to the other. You can work around the problem by adding another as:

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

@massimocode
Copy link
Author

Hi Anders, thanks for your reply and also for all the hard work on C#/TypeScript!

When I raised this I was basically thinking to myself "ok so I have defined the 'union' property with a valid value of a type that it allows (Bar or Baz)... just as if I were to define only the foo property with a valid value of a type that it allows (string)"... i.e. I feel like the logic for the "as" keyword doesn't check if the value I'd given to the "union" property was a valid Bar or a valid Baz... instead it seems to be looking for something of type "Bar | Baz" (as you stated) which isn't really going to exist in an object literal without some kind of manual coercion

I'm not really sure if it needs a new kind of type relation or if it's just an issue with the existing type relations, but just wanted to state my thought trail in case it was of any help

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Feb 19, 2016
@mhegazy mhegazy closed this as completed Feb 19, 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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants