You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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:
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
From what I understand, the as keyword allows you to omit properties... but it doesn't seem to work when union types are involved...
I've tested on TypeScript 1.7.5 (current release) and typescript@next (Version 1.9.0-dev.20160205)
The text was updated successfully, but these errors were encountered: