# Trying node-red-contrib-ts

**URL:** <https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956>\
**Category:** General\
**Created:** [4 September 2025 19:58 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956 "2025-09-04T19:58:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [4 September 2025 19:58 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/1 "2025-09-04T19:58:53Z")

</div>

Trying node-red-conrib-ts

@benoitadam what does it take to make this error disappear in node-red-contrib-ts.

```auto
let msg1 = {}
let invar = msg.payload

msg1.payload = invar
return msg1

```

I use that a lot in function nodes.

```auto
[{"id":"02b27754f6032fe8","type":"typescript","z":"7addd5819f6fd8a6","name":"typescript 1","func":"let msg1 = {}\nlet invar = msg.payload\n\nmsg1.payload = invar\nreturn msg1\n","outputs":1,"timeout":0,"useVm":false,"updated":"1757014932549","declare":"","noerr":1,"initialize":"","finalize":"","libs": [],"x":530,"y":180,"wires":[["277a0382feeee259"]]},{"id":"95d2eae65ce966d2","type":"inject","z":"7addd5819f6fd8a6","name":"","props": [{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType ":"date","x":180,"y":180,"wires":[["02b27754f6032fe8"]]},{"id":"277a0382feeee259","type":"debug","z":"7addd5819f6fd8a6","name":"debug 75","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":180,"wires":[]},{"id":"cb6b5379cc95ab76","type":"function","z":"7addd5819f6fd8a6","name":"function 25","func":"let msg1 = {}\nlet invar msg.payload\n\nmsg1.payload = invar\nreturn msg1\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":300,"wires":[["9c77eb4e33 14e145"]]},{"id":"16aa73374cef7423","type":"inject","z":"7addd5819f6fd8a6","name":"","props":[{"p":"payload"},{"p":"topi c","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x": 180,"y":300,"wires":[["cb6b5379cc95ab76"]]},{"id":"9c77eb4e3314e145","type":"debug","z":"7addd5819f6fd8a6","name":"debug 76","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":300,"wires":[]},{"id":"23a4d61f6d27b6a7","type":"global-config","env":[],"modules":{"node-red-contrib-ts":"1.2.3"}}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 September 2025 20:36 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/2 "2025-09-04T20:36:46Z")

</div>

What error are you referring to? We cannot see one.

You might get a LINT warning about the use of let instead of const though since you are not resetting the values of either msg1 or invar.

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [5 September 2025 06:32 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/3 "2025-09-05T06:32:06Z")

</div>

Best shown with a few pictures.

 ![pic1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/969660cd17c98201895d304cef1ebc5eccfc64b1.png)  
 ![pic2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e0e8a02b4fee779ed174af762a6566eaee34812.png)

But I've figured out that msg1.payload needs to be declared first.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 September 2025 06:51 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/4 "2025-09-05T06:51:22Z")

</div>

(Sorry just passing by)

~~Searching the library ([Library - Node-RED](https://flows.nodered.org/search?type=node)) I can't find any node  
 `node-red-contrib-ts`~~

The code posted is ..... sort of confusing.

It gets the payload of the incoming message, makes it a variable, then sets `msg1.payload` to that variable, then sens on that message.

I'm guessing that is to strip off any other stuff from the message?

Why are you doing this?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 September 2025 08:34 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/5 "2025-09-05T08:34:34Z")

</div>

I thought it had been said the Typescript is a superset of JS and that existing JS code would run without modification.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 September 2025 09:55 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/6 "2025-09-05T09:55:05Z")

</div>

🤣

Not quite true is it. TS is a lot more strict. This has advantages in some situations with complex code and large teams.

The correct code would be:

```js
const msg1 = {
  payload: msg.payload
}

return msg1

```

Though this does break the message chain which can be problematic. You will get a new msg id and will loose any other context such as msg.topic.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 September 2025 10:39 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/7 "2025-09-05T10:39:01Z")

</div>

If what @TotallyInformation is true and it is _better_ to strip away _other stuff_ before sending a message to it....

Doesn't that make in a bit of a naughty node.

Most other nodes accept complicated message packets.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 September 2025 11:21 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/8 "2025-09-05T11:21:03Z")

</div>

I think (hard to say without actually seeing the error), it is only Monaco that is highlighting the fact that the payload property is being assigned but it hasn't been defined.

This is pretty much at the heart of what I've been saying about TS. In an enterprise setting with loads of coders and a big code-base, accurately defining the structure of your objects is important for avoiding issues. But typically this is absolutely the opposite of what you want for a quick function node.

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [6 September 2025 06:42 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/9 "2025-09-06T06:42:02Z")

</div>

The solution for me was

```auto
//let msg1 = {}
const msg1 = {
payload: {},
}

let invar = msg.payload

msg1.payload = invar
return msg1

```

For @Trying_to_learn  
I use this setup. to remove all unnecessary stuff before sending it on. This example was boiled down to the bare minimum.

---

<div class="post-metadata">

**Author:** ![benoitadam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/benoitadam/32/102997_2.png) [@benoitadam](https://discourse.nodered.org/u/benoitadam)\
**Post date:** [6 September 2025 08:31 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/10 "2025-09-06T08:31:31Z")

</div>

**Thanks for your question about typing in node-red-contrib-ts! 🚀**

There are indeed several ways to handle TypeScript typing in your Node-RED functions depending on your needs:

**1) Explicit `any` type (straightforward approach)**

 ![Capture d’écran 2025-09-06 à 10.14.23](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4bf4cf802053f9f97d8ebc9fa7f2885db392c238.png)

```auto
let msg1: any = {}
let invar = msg.payload
msg1.payload = invar
return msg1

```

**2) Disable type checking (for legacy code)**

 ![Capture d’écran 2025-09-06 à 10.15.28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c59f7f15ddccc23c81687f46a4a16467285e454e.png)

```auto
// @ts-nocheck
let msg1 = {}
let invar = msg.payload
msg1.payload = invar
return msg1

```

**3) Use the built-in MsgBase interface**

 ![Capture d’écran 2025-09-06 à 10.26.08](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0ac0d062a252140a58820e1c0522ee29a1b524c7.png)

**4) The `Msg` interface can be customized**

 ![Capture d’écran 2025-09-06 à 10.17.45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91a67288eb1557f796280301d1b7685ad31464a4.png)

```auto
interface Msg {
    topic?: string
    payload?: any
    [key: string]: any
}

let msg1: Msg = {}
let invar = msg.payload
msg1.payload = invar
return msg1

```

## 💡 Tip

For a smooth migration from JavaScript, approach **#1 with `any`** is often the most practical. For more robust code, approach **#4 with interfaces** offers a good balance between flexibility and type safety.

Feel free to ask if you have more questions about using TypeScript with Node-RED!

#NodeRED #TypeScript #OpenSource

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 September 2025 08:31 UTC](https://discourse.nodered.org/t/trying-node-red-contrib-ts/98956/11 "2025-09-20T08:31:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
