使用typeORM的时候:
比如:定义了一个Entity:
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
firstName: string
@Column()
lastName: string
@Column()
age: number
}
有2个报错:
1、
作为表达式调用时,无法解析属性修饰器的签名。
类型“undefined”的参数不能赋给类型“Object”的参数。ts(1240)
(alias) PrimaryGeneratedColumn(): PropertyDecorator (+5 overloads)
import PrimaryGeneratedColumn
Column decorator is used to mark a specific class property as a table column.
2、
属性“firstName”没有初始化表达式,且未在构造函数中明确赋值。ts(2564)
(property) User.firstName: string
请问是需要如何进行修复呢?还是说这个本来就是这样定义的?
更新:
我这边是有添加AI bot回复的信息的,但是还是有提示报错信息:
{
"compilerOptions": {
...
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"sourceMap": true
===
编辑:1
package.json
{
"name": "electron-test-05-typeorm",
"version": "1.0.0",
"description": "An Electron application with React and TypeScript",
"main": "./out/main/index.js",
"author": "example.com",
"homepage": "https://electron-vite.org",
"scripts": {
"format": "prettier --write .",
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
"typecheck:node": "tsc --noEmit -p tsconfig.node.json --composite false",
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
"typecheck": "npm run typecheck:node && npm run typecheck:web",
"start": "ts-node src/index.ts",
"dev": "electron-vite dev",
"build": "npm run typecheck && electron-vite build",
"postinstall": "electron-builder install-app-deps",
"build:unpack": "npm run build && electron-builder --dir",
"build:win": "npm run build && electron-builder --win",
"build:mac": "electron-vite build && electron-builder --mac",
"build:linux": "electron-vite build && electron-builder --linux",
"typeorm": "typeorm-ts-node-commonjs",
"rebuild": "electron-rebuild -f -w better-sqlite3"
},
"dependencies": {
"@electron-toolkit/preload": "^3.0.1",
"@electron-toolkit/utils": "^3.0.0",
"@electron/rebuild": "^3.6.0",
"await-to-js": "^3.0.0",
"better-sqlite3": "^11.2.1",
"electron-rpc-api": "^10.0.0",
"inversify": "^6.0.2",
"pg": "^8.4.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"staatshelfer": "^0.0.1",
"typeorm": "0.3.20",
"zustand": "^4.5.5"
},
"devDependencies": {
"@electron-toolkit/eslint-config-prettier": "^2.0.0",
"@electron-toolkit/eslint-config-ts": "^2.0.0",
"@electron-toolkit/tsconfig": "^1.0.1",
"@types/node": "^16.18.107",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"electron": "^31.0.2",
"electron-builder": "^24.13.3",
"electron-vite": "^2.3.0",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.3",
"prettier": "^3.3.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"ts-node": "10.9.1",
"typescript": "4.5.2",
"vite": "^5.3.1"
}
}
node版本:
$ node -v
v20.12.1
第一个问题 AI 给答案了,照着改 tsconfig 就好。
第二个问题是你把属性的类型声明成非空的了,但又没给初始值。
要么你声明成可空的:
要么加非空断言符:
看你自己业务里这个字段究竟是否可以置为空。
P.S. AI 给的改法也不是不行,但对于定义 ORM 的 Entity 来说并不是最优解。