-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Emit of union type metadata in nullStrictMode is always Object #12703
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
Comments
Union types are emitted as |
I disagree. I don't argue that all union types should behave like I have mentioned, but only when single type unioned with null or undefined or both. if you still think it is working as intended. please mark this future as Breaking change Till the use of strictNull I could do the following: class A {}
class B
{
@dec()
field: A
}
const variable: B;
variable.field = null; and the code would compile as expected and field type would be emitted as expected allowing me at Runtime to expect it and use it. now, when the strict flag is on, all fields that can accept null or undefined, emitted as Object type, which become the metadata unusable in my case and I guess for others who relied on this behavior |
if you are not using |
I understand that, the null | undefined addition was seemed straight forward change, but now cause of the union definition, the metadata loosing it purpose, and it breaking my code that relies on this emit. I looking a way to enjoy both worlds :-) |
why is |
As I see it, Foo | number union is splitted to two different valid types in javascript world. number is primitive type and Foo is descendant of Object type. Foo | null union is a more special case. if we try to split it, we will find only Foo type and null hint for compiler, tells that field defined with that union can have valid Foo type or being null reference. from JavaScript perspective, null is not a Object descendant or primitive type. this definition could be the same: let defined: @null() decorator that hints compiler, field can have null reference then in theory this definition should be equal: class A
{
field: Foo | null | undefined;
}
class B
{
@Null()
@Undefined()
field: Foo;
} on both cases the compiler will understand the hint for allowing null | undefined definitions and code analysis would work, but in A type the union will emit Object type meta, in B the emit will be B type for my opinion, the null, undefined tokens should be ignored only on metadata emit, they should still continue to participate in all other aspects for the union analysis logic |
turns out that when using union types with `undefined` the compiler will save the type as `Object` (more info: microsoft/TypeScript#12703), which isn't good for typeorm. Instead, mark nullable props as optional.
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Expected behavior:
Actual behavior:
The text was updated successfully, but these errors were encountered: