ddd-forum
ddd-forum copied to clipboard
Enabling strict in tsconfig breaks many things
Enable the strict mode in the tsconfig and check the errors. Seems we are not adhering to type safety in many places. I'm a novice and learning from your repo. Not really sure whether enabling strict mode is really needed or not.
@email2vimalraj I'm having a similar experience. One example is in https://github.com/stemmlerjs/ddd-forum/blob/master/src/shared/core/Result.ts.
Typescript Errors for Result.ts
src/shared/core/Result.ts(21,5): error TS2322: Type 'string | T | undefined' is not assignable to type 'string | T'.
Type 'undefined' is not assignable to type 'string | T'.
src/shared/core/Result.ts(22,5): error TS2322: Type 'T | undefined' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'T | undefined'.
Type 'undefined' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
src/shared/core/Result.ts(43,32): error TS2345: Argument of type 'null' is not assignable to parameter of type 'string | U | undefined'.
@email2vimalraj I'm having a similar experience. One example is in https://github.com/stemmlerjs/ddd-forum/blob/master/src/shared/core/Result.ts. Typescript Errors for Result.ts
I'm new to Typescript and OOP so say whatever I will with a large grain of salt. In any case I wanted to jot down my thoughts of why this might be for my own learning with the hopes that someone might correct me.
The parameter 'value' in the constructor (line 8) is marked optional with the question mark, which makes it possible that it gets passed with undefined. In the class definition of Result<T>, the private variable _value requires it to be T. Is it the case that T is not able to 'capture' undefined values as well?
The same counts for the error parameter (line 18).
In the public static function ok<U> (value?: U): Result<U> returning null as parameter of the Result type probably also does not work because null means that the parameter is implicitly empty vis-a-vis set but undefined. Setting this to 'undefined' gets rid of the error for me.
@email2vimalraj I'm having a similar experience. One example is in https://github.com/stemmlerjs/ddd-forum/blob/master/src/shared/core/Result.ts. Typescript Errors for Result.ts
I'm new to Typescript and OOP so say whatever I will with a large grain of salt. In any case I wanted to jot down my thoughts of why this might be for my own learning with the hopes that someone might correct me.
The parameter 'value' in the constructor (line 8) is marked optional with the question mark, which makes it possible that it gets passed with undefined. In the class definition of Result, the private variable _value requires it to be T. Is it the case that T is not able to 'capture' undefined values as well?
The same counts for the error parameter (line 18).
In the public static function
ok<U> (value?: U): Result<U>returning null as parameter of the Result type probably also does not work because null means that the parameter is implicitly empty vis-a-vis set but undefined. Setting this to 'undefined' gets rid of the error for me.
I'm sorry for the very late reply. I wanted to thank you for pointing this out. The issue doesn't exist in the frontend version of the file. Adding null to the possible types of error and casting both this.error and this._value as T resolves the issue:
Frontend https://github.com/stemmlerjs/ddd-forum/blob/ea393502396c91b7d85fdcc664b158de683a0bf2/public/app/src/shared/core/Result.ts#L8 https://github.com/stemmlerjs/ddd-forum/blob/ea393502396c91b7d85fdcc664b158de683a0bf2/public/app/src/shared/core/Result.ts#L18-L19 Backend https://github.com/stemmlerjs/ddd-forum/blob/ea393502396c91b7d85fdcc664b158de683a0bf2/src/shared/core/Result.ts#L8 https://github.com/stemmlerjs/ddd-forum/blob/ea393502396c91b7d85fdcc664b158de683a0bf2/src/shared/core/Result.ts#L18-L19
This raises two questions for me:
- Why use
this._valueinstead ofthis.valuewhen TypeScript offers theprivatekeyword (from SOLID Book) - How do we keep this DRY?