Skip to content

Conversation

@serjKim
Copy link

@serjKim serjKim commented May 31, 2020

When strictNullCheks: true is used with union type e.g. string | null, typescript emits the 'design:type' metadata as Object rather than string. Example:

class A {
    @dataMember()
    public foo: string | null;
}

const a = deserialize({ foo: 'asd' }, A); // Causes a warning: Deserialization: Primitive type 'asd'' passed to the object for the property 'foo'. It is impossible to determine whether object type is 'any' or 'Object' or 'interface'. Try to avoid 'any' types

Indeed,

...
let A = /** @class */ (() => {
    class A {
    }
    __decorate([
        santee_dcts_1.dataMember(),
        __metadata("design:type", Object) // <-- Object! But must be String
    ], A.prototype, "foo", void 0);
    return A;
})();
const a = santee_dcts_1.deserialize({ foo: 'asd' }, A);

Another one example:

const dec = (target: any, key: string): undefined => undefined;

class A {
    @dec
    public property: string | null;
}

const obj = Reflect.getMetadata('design:type', A.prototype, 'property');

console.log(obj); // Object

There was some discussion microsoft/TypeScript#18509 (comment)
It appears to be the behavior is by design.

I suppose to add an additional fieldType prop to dataMember as a workaround to be able to pass a ctor explicitly:

class A {
    @dataMember({ fieldType: String })
    public foo: string | null;
}

const a = deserialize({ foo: 'asd' }, A);

serjKim added 2 commits May 31, 2020 22:31
- Update gulp to 4.x for compatibility with node 12.x (Fix: ReferenceError: primordials is not defined)
- Update typescript to 3.9.3
Using strictNulChecks
- Remove deprecated tsd package in favor of @types
- Fix gulpfile according to 4.x api
Use gulp.series() instead of []
Forward references: Define tslint task before using. gulp can't refer to task that haven't neen defined yet.
@serjKim
Copy link
Author

serjKim commented May 31, 2020

@santee @AlexeyVykhrystyuk-GB FYI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant