question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incompatible with typescript strict mode

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Kononnablecommented, Apr 29, 2020

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).

5reactions
colthreepvcommented, Apr 29, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Strict Mode: having error on BehaviouralSubject (null)
You provide a generic parameter to tell typescript that the type used by the class may be either null or Customer. Try the...
Read more >
How strict is Typescript's strict mode? - DEV Community ‍ ‍
According to the docs, when Typescript strict mode is set to on, it will validate your code ... Types of property 'length' are...
Read more >
TSConfig Reference - Docs on every TSConfig option
ECMAScript strict mode was introduced in ES5 and provides behavior tweaks to the runtime of the JavaScript engine to improve performance, and makes...
Read more >
Angular compiler options
When true , the compiler does not look at the TypeScript version and does not report an error when an unsupported version of...
Read more >
TypeScript errors and how to fix them
error TS1117: An object literal cannot have multiple properties with the same name in strict mode. Broken Code ❌. 1 2 3 4...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found