Skip to content

Incompatible with typescript strict mode #4018

Closed

Description

@colthreepv

Using typeorm in a strict: true project makes it behave in the wrong way.

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[x] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[x] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

$ typeorm init --db sqlite
$ npm i
  1. Add strict: true in tsconfig.json
  2. Fix all properties in src/User.ts from <property>: <type> to <property>!: <type>
  3. ts-node src/index.ts

Result:

$ ts-node src/index.ts

C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:240
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:

9     user.firstName = "Timber";
      ~~~~~~~~~~~~~~

src/index.ts(9,5): error TS2322: Type '"Timber"' is not assignable to type 'boolean'.

10     user.lastName = "Saw";
       ~~~~~~~~~~~~~

src/index.ts(10,5): error TS2322: Type '"Saw"' is not assignable to type 'boolean'.

11     user.age = 25;
       ~~~~~~~~

src/index.ts(11,5): error TS2322: Type '25' is not assignable to type 'boolean'.

    at createTSError (C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:240:12)
    at reportTSError (C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:244:19)
    at getOutput (C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:360:34)
    at Object.compile (C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:393:11)
    at Module.m._compile (C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\valerio\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)

Activity

  1. Kononnable commented on Apr 27, 2019

    @Kononnable
    Contributor

    First of all I wouldn't call adding ! as fixing properties. You're just telling compiler that property value will never be undefined which isn't true.
    However I tried to reproduce the issue and failed. It worked as expected for me.
    It looks like you've placed ! in the wrong place(and made a conversion to boolean) or something similar.

    Please check your code and if you don't see anything suspicious paste code here(or put it under github repo).

  2. colthreepv commented on Apr 29, 2019

    @colthreepv
    Author

    I will try to reproduce inside a docker container.
    If so will create a repository.

    I written entities this way

    @Column()
    someField!: string
    

    It seemed to make typescript happy

    Thanks for looking into it

  3. ejose19 commented on Apr 29, 2020

    @ejose19
    Contributor

    @Kononnable from your comment I get that you don't recommend using ! on Entity definition, is that correct? So what do you do instead to work with "strict": true? I see "strictPropertyInitialization": false can be used but the drawback is that all non entity classes will miss that feature.

  4. Kononnable commented on Apr 29, 2020

    @Kononnable
    Contributor

    It really depends on 'how strict' you want to be. The only options with check enabled is lying to compiler(!) or adding (| undefined or ?) to every column. Second option is rarely a good solution, because we would have to always check if value is undefined.
    As for the lying part - most of the times it will work as developer wants. However don't always get this value, e.g. we can disable it in a select(or not join the table if it is done on a relationship) and value returned will be undefined. And here comes a dilemma(and personal preference) - should we enable the check and lie to the compilator - when we face this 'bug' no obvious solution to why value is undefined will be find, or disable check and by looking on tsconfig we should be able to tell how are undefined values introduced here.
    When we look just at the entity reason would be obvious, but if undefined value was already copied somewhere it might be much harder to knew why it is set to undefined while type system tells different story.

    Typeorm is not really compatible with strictPropertyInitialization, it was designed long before such feature came int existence. It can work with this check(with those workarounds), but it's far from ideal.

    Sidenote: At the time of original reply I didn't like strictPropertyInitialization very much. There was so many dependencies written without that rule, that I didn't see much value in enabling this setting(it was common that property which could not be initialized to not be marked as optional). Nowadays I don't know what I would choose. This strict check isn't bad, but it's problematic with code which initialize all of the fields in most cases(but there is still a possibility of field being uninitialized). The ideal solution would be to fix this in typeorm, but I don't see it happening anytime soon(or ever).

  5. ejose19 commented on Apr 29, 2020

    @ejose19
    Contributor

    @Kononnable Thanks for your detailed reply, regarding select | relations, would be nice if typeorm when using a select modified the return type to be Pick<Entity, selectedValues> (and same for relations, innerJoin* and leftJoinAndMapMany would convert the relation property from ? to !)

    Since this is not the case and may not end up being implemented here, one solution would be doing those cast manually after querying, which depending on app complexity could be a decent workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions