# Incorrect optional typescript declaration generation?

**URL:** <https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045>\
**Category:** Developers\
**Created:** [July 22, 2021, 3:29pm UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045 "2021-07-22T15:29:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [July 22, 2021, 3:29pm UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/1 "2021-07-22T15:29:26Z")

</div>

Given following Motoko code:

```auto
actor Deck {
    private type DeckData = {
        header: ?Text;
    };
}

```

The SDK (v0.7.7) generates following type declaration:

```auto
export interface DeckData {
  'header' : [] | [string],
}

```

Isn’t that incorrect?

Shouldn’t be `header?: string` or `header: string | null`?

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [August 3, 2021, 7:32am UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/2 "2021-08-03T07:32:39Z")

</div>

The optional value JS binding is defined this way:

> **[Internet Computer | Documentation](https://sdk.dfinity.org/docs/candid-guide/candid-types.html#type-opt)**
>
> Documentation for the internet computer.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 3, 2021, 7:45am UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/3 "2021-08-03T07:45:47Z")

</div>

> The `opt t` type contains all the values of type `t` , plus the special `null` value. It is used to express that some value is optional, meaning that data might be present as some value of type `t` , or might be absent as the value `null` .

Yes, that’s why I don’t get why it generates Typescript declaration with arrays `[] | [t]` instead of `t | null`

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [August 3, 2021, 8:02am UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/4 "2021-08-03T08:02:04Z")

</div>

I suspect its because the your suggested translation doesn’t work as intended for nested option types.

?? T would also translate to T\* | null, conflating the two source null values.

But that doesn’t mean there isn’t a better translation that’s possible.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 3, 2021, 9:21am UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/5 "2021-08-03T09:21:11Z")

</div>

> [@claudio](#):
>
> suggested translation

(noob question) do you mean, in my example, `header: ?Text;`?

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [August 3, 2021, 2:27pm UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/6 "2021-08-03T14:27:03Z")

</div>

I also think there’s a better approach for these bindings, but I haven’t done a formal proposal on them yet. Optional arguments are the least JavaScriptey design of them all now, though

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 3, 2021, 2:33pm UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/7 "2021-08-03T14:33:25Z")

</div>

All clear. Go for it, I would really not mind more JavaScript-like optional bindings.

Like I am looking forward to remove my helper 😉.

```auto
const fromNullable = <T>(value: [] | [T]): T | undefined => {
  return value?.[0];
}

```

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [November 28, 2021, 1:03am UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/8 "2021-11-28T01:03:08Z")

</div>

Yeah, it’s better to return `T | undefined` instead of `T | null`, because the JavaScript `null` can also represent the Motoko` ()`, so if you had to return `?()` in Motoko, that would map to `null | null` in JS, which wouldn’t work.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [November 28, 2021, 1:51am UTC](https://forum.dfinity.org/t/incorrect-optional-typescript-declaration-generation/6045/9 "2021-11-28T01:51:49Z")

</div>

Do you know if and when the TS types for Optional will ever be changed to be `T | undefined` by default when `dfx build` is run? Also, is there an easy way for devs to override that locally until it’s officially changed?

I’m curious why it wasn’t done like that in the beginning.
