環境
Typescript 4.5.2
Express 4.16.1
TypeORM 0.3.17
postgreSQL 15
状況
TypeORMのEntityでnullableなDate型を定義したいのですが、以下のコードでエラーが出ました。
@Column({ nullable: true, type: "Date" })
time!: Date|null;
エラー内容
TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '{ nullable: boolean; type: string; }' is not assignable to parameter of type '(type?: any) => Function'.
Object literal may only specify known properties, and 'nullable' does not exist in type '(type?: any) =>
Function'.
20 @Column({ nullable: true, type: "Date" })
ここで調査してみると、TypeORMにはDate型はなく、string型で表現されるらしいです。
https://github.com/typeorm/typeorm/issues/2176
そこでstring型に変更しましたがまたエラーが出ます。
TypeORM connection error:
DataTypeNotSupportedError: Data type "Object" in "Schedule.time" is not supported by "postgres" database.
これについてはこれと同じくtype:'varchar'を明示してあげることでうまくいきました。
https://stackoverflow.com/questions/64350794/typeorm-message-data-type-object-in-is-not-supported-by-postgr