无法将未知类型分配给具有 typeorm 的实体
Unable to assign the type unknown to an entity with typeorm
我在将 TypeOrm 与特定项目的打字稿一起使用时遇到问题。打字稿似乎无法识别来自 typeorm 实体的类型。
@Entity({
name: "users",
synchronize: false
})
export default class User {
private tempPassword:string | undefined;
@PrimaryGeneratedColumn()
id!: number;
@Column({
type: "text"
})
name!: string;
...
}
上面的代码是我的模型。所以当我尝试时:
let user: UserModel;
try {
user = await getRepository("User").findOne({name: "test"});
我在用户变量上有以下错误:
(这意味着:无法将未知类型分配给用户类型)
tsconfig.json
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "build", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
/* Strict Type-Checking Options */
"strict": true,
"esModuleInterop": true,
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"resolveJsonModule": true,
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
}
是否是因为 tsconfig json 中缺少某些内容?非常感谢
你有 strict: true
意味着你还必须检查 null
/ undefined
.
let user: UserModel | undefined
;
编辑:
我认为 getRepository(string)
签名 returns unknown
(如果需要,您可以声明其结果 - as User
)。试试,getRepository(User)...
我在将 TypeOrm 与特定项目的打字稿一起使用时遇到问题。打字稿似乎无法识别来自 typeorm 实体的类型。
@Entity({
name: "users",
synchronize: false
})
export default class User {
private tempPassword:string | undefined;
@PrimaryGeneratedColumn()
id!: number;
@Column({
type: "text"
})
name!: string;
...
}
上面的代码是我的模型。所以当我尝试时:
let user: UserModel;
try {
user = await getRepository("User").findOne({name: "test"});
我在用户变量上有以下错误:
(这意味着:无法将未知类型分配给用户类型)
tsconfig.json
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "build", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
/* Strict Type-Checking Options */
"strict": true,
"esModuleInterop": true,
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"resolveJsonModule": true,
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
}
是否是因为 tsconfig json 中缺少某些内容?非常感谢
你有 strict: true
意味着你还必须检查 null
/ undefined
.
let user: UserModel | undefined
;
编辑:
我认为 getRepository(string)
签名 returns unknown
(如果需要,您可以声明其结果 - as User
)。试试,getRepository(User)...