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
As of typescript version 3.7, there's a flag called useDefineForClassFields. If your build target is es2022 or above (including esnext), it defaults to true, but otherwise defaults to false.
If you are compiling typescript with the useDefineForClassFields as true and if you use the example code from the typedorm docs to define an entity that uses attribute auto generation, you'll get an error when you run entityManager.create(entity).
If useDefineForClassFields is true, you'll get an error complaining about how the id field is empty. The root issue is that the toDynamoEntity on BaseTransformer doesn't run auto-generation on any entity keys that already have a key (even if the value is undefined).
Suggested Changes
Seems like the code in the readme should would be compatible with the defaults of future build targets.
We could either:
modify the logic in BaseTransformer.toDynamoEntity to auto generate values for keys that don't have undefined values, rather than only for object keys that aren't defined at all. The downside is that this would be a breaking change for anyone who is intentionally setting an attribute as undefined to prevent it from being auto-generated. I can't imagine someone doing this, but maybe someone has more context here.
Modify the readme to show how to use the declare keyword to prevent the undefined value being set for that key.
Now you're telling typescript not to assign undefined to that key. So the attribute auto-generation in entityManager.create(new FooEntity()) call would work as expected.
Summary
As of typescript version 3.7, there's a flag called
useDefineForClassFields
. If your build target ises2022
or above (includingesnext
), it defaults to true, but otherwise defaults to false.If you are compiling typescript with the
useDefineForClassFields
as true and if you use the example code from the typedorm docs to define an entity that uses attribute auto generation, you'll get an error when you runentityManager.create(entity)
.https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier
Background Info:
Suppose you have this code.
useDefineForClassFields
is false, you'll getFooClass {}
useDefineForClassFields
is true, you'll getFooClass { id: undefined }
So suppose you have typedorm code like this
If
useDefineForClassFields
is true, you'll get an error complaining about how theid
field is empty. The root issue is that thetoDynamoEntity
onBaseTransformer
doesn't run auto-generation on any entity keys that already have a key (even if the value is undefined).Suggested Changes
Seems like the code in the readme should would be compatible with the defaults of future build targets.
We could either:
modify the logic in
BaseTransformer.toDynamoEntity
to auto generate values for keys that don't have undefined values, rather than only for object keys that aren't defined at all. The downside is that this would be a breaking change for anyone who is intentionally setting an attribute as undefined to prevent it from being auto-generated. I can't imagine someone doing this, but maybe someone has more context here.Modify the readme to show how to use the
declare
keyword to prevent the undefined value being set for that key.For example, if you add the
declare
...Now you're telling typescript not to assign
undefined
to that key. So the attribute auto-generation inentityManager.create(new FooEntity())
call would work as expected.See also
#338
The text was updated successfully, but these errors were encountered: