Git Product home page Git Product logo

Comments (3)

thiagodebastos avatar thiagodebastos commented on August 11, 2024

@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'.

from ddd-forum.

pandu-supriyono avatar pandu-supriyono commented on August 11, 2024

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

from ddd-forum.

thiagodebastos avatar thiagodebastos commented on August 11, 2024

@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

public constructor (isSuccess: boolean, error?: T | string | null, value?: T) {

this.error = error as T;
this._value = value as T;

Backend
public constructor (isSuccess: boolean, error?: T | string, value?: T) {

this.error = error;
this._value = value;

This raises two questions for me:

  1. Why use this._value instead of this.value when TypeScript offers the private keyword (from SOLID Book)
  2. How do we keep this DRY?

from ddd-forum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.