Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM import of Relation fails

I am using typeorm in a nodejs project with esm. I need to use the Relation wrapper to avoid circular dependencies.

I get this error when i try to use the relation wrapper:

import { Column, Entity, OneToOne, PrimaryGeneratedColumn, Relation } from 'typeorm';
                                                           ^^^^^^^^
SyntaxError: The requested module 'typeorm' does not provide an export named 'Relation'

The code file:

import { Column, Entity, OneToOne, PrimaryGeneratedColumn, Relation } from 'typeorm';
import { Vehicle } from './vehicle';

@Entity({ name: 'vehicle_default_values' })
export class VehicleDefaultValues {
    @PrimaryGeneratedColumn({ unsigned: true })
    id: number;

    @Column({ type: 'int', unsigned: true, unique: true })
    model: number;

    @Column({ type: 'float', default: 550 })
    maxFuel: number;

    @Column({ type: 'boolean', default: false })
    isElectric: boolean;

    @Column({ type: 'float', default: 0.008 })
    fuelConsumption: number;

    @Column({ type: 'mediumint', unsigned: true, default: 200 })
    maxSpeed: number;

    @Column({ type: 'mediumint', unsigned: true, default: 50 })
    trunkVolume: number;

    @Column({ type: 'text', nullable: true })
    vehicleExtras: string;

    @OneToOne(() => Vehicle, (vehicle) => vehicle.vehicleDefaultValues)
    vehicle: Relation<Vehicle>;
}

Used version of typeorm: 0.3.7

like image 417
Nils van Lück Avatar asked Oct 30 '25 21:10

Nils van Lück


1 Answers

This is probably happening because the types are not included when converting the file to javascript. The imports Column, Entity, OneToOne, PrimaryGeneratedColumn are all functions, while the import Relation is a type.

With typescript 3.8 or newer you can explicitly tell that you are importing types like this:

import { Column, Entity, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import type { Relation } from 'typeorm';

or with typescript 4.5 or newer you can do the same like this:

import { Column, Entity, OneToOne, PrimaryGeneratedColumn, type Relation } from 'typeorm';
like image 121
Henri Kellock Avatar answered Nov 01 '25 12:11

Henri Kellock