1

I found this issue in typescript. I have following example:

type AirplaneDTO = {
    color?: string|null
    owner?: string|null
}

type Airplane = {
    color: string
    owner: string
}

function isValidAirplane(airplane: AirplaneDTO): boolean {
    return !!(airplane.color && airplane.owner);
}

function getValidAirplane(airplane: AirplaneDTO): Airplane|undefined {
    if (!isValidAirplane(airplane)) {
        return;
    }
    return {
        color: airplane.color,
        owner: airplane.owner,
    }

}

As you can see, I am converting data from AirplaneDTO to Airplane. I don't want to set Airplane object if some of the fields in AirplaneDTO are null or undefined.

As I am doing this check if all the fields inside are set, TS does not recognize the "isValidAirplane" function-call and gives me following Error: Type 'string | null | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.(2322)

You can try my code and Error here: https://www.typescriptlang.org/play?ssl=28&ssc=1&pln=1&pc=1#code/C4TwDgpgBAgglgJzAGwIYDsIBEAqB5KAXigG8AoKSqAYwHtlaEB+ALigGdgE50BzAH3QBXZMgpVaAd0zM2nbn0EixAXzJlQkWIhQZoxclRr1Gcrj17jKUmWYWW1ZAGZD01YHFroocdgDVUZDgAE3gkNEwAClQdCIg2MN1MXDwASjYAI1p6CAxSKygECGAhBG8AQnLo2L0AOjoGBCgAMmaoGPC6mwgEVIBuMkcXNw8vKF5igKDQmqiOpPjtTuT8dKWF-ldgiCceCGD8ozgnKEjy3ymQxLjq5YhU1MOjKiKSsoGjNSNX0u9DZ8oDVM7VmEHqJgQABoCkZugg2PM4rU4dDPupHOogA

Is this an known Typescript issue or did I do something wrong here?

5
  • 1
    If it's already a valid "Airplane", why not return it instead of undefined? Commented Jan 23, 2024 at 15:27
  • 1
    You're close, just make isValidAirplane a type predicate by changing the return type from boolean to airplane is Airplane. More here. Commented Jan 23, 2024 at 15:28
  • 1
    Just FWIW, you don't need to create a new object in getValidAirplane if you don't want to. You know the object that was passed in is a valid Airplane after the isValidAirplane call, so you could just return it. Commented Jan 23, 2024 at 15:30
  • @Pointy its not a valid Airplane if one of the fields inside are null or undefined. Commented Jan 23, 2024 at 15:44
  • @MarkusB - Here's your playground updated with the change I mentioned above. Commented Jan 23, 2024 at 15:50

1 Answer 1

0

I would recommend to parse, not validate.

Thus instead of isValidAirplane you have the following function:

function parseAirplane(airplane: AirplaneDTO): Airplane | undefined {
    const { color, owner } = airplane
    if (color && owner) {
      return { color, owner }
    }
}

Instead of returning undefined, you can also return an Error type...

Sign up to request clarification or add additional context in comments.

3 Comments

I don't agree; the word "parse" really means (or, should mean) to extract structure from plain text. Also, either "color" or "owner" could be empty in a valid airplane, for all we know.
@Pointy did you read the linked article? (and according to OPs type definitions, a valid Airplane has no optional fields...
This is very nice with just two parameters to check. but if there are much more fields and checks I want to make instead of if (color && owner) I want to extract it in an own function... And this is my problem, that TS doesn't recognize that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.