Do we still need to define the type explicitly when it's nullable #503
-
Using v8.7.6 If I recall correctly, in the past we'd have to use But my question is: do we still need to explicitly type destination's type if it has an union with See demo codeimport { createMap, createMapper, extend, nullSubstitution, forMember } from '@automapper/core';
import { classes, AutoMap } from '@automapper/classes';
const mapper = createMapper({
strategyInitializer: classes(),
});
class S {
@AutoMap(() => String) // Doesn't work
// @AutoMap() // Does works
foo: string | null
}
class D {
@AutoMap()
foo?: string
}
class D2 extends D {}
createMap(mapper,
S, D,
forMember(
d => d.foo,
nullSubstitution(undefined),
)
)
createMap(mapper,
S, D2,
extend(S, D)
)
const s = new S()
s.foo = null
console.log(
mapper.map(s, S, D) ,
mapper.map(s, S, D2) ,
)
/*
D { foo: undefined }
D2 { foo: null }
*/ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
ping @nartc 😢 |
Beta Was this translation helpful? Give feedback.
-
Hi @micalevisk, it was a bug with |
Beta Was this translation helpful? Give feedback.
Hi @micalevisk, it was a bug with
extend()
. I kept the old behavior (you still need to explicitly set a type for Null union). Check out the latest version