Trying node-red-contrib-ts

Trying node-red-conrib-ts

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

let msg1 = {}
let invar = msg.payload

msg1.payload = invar
return msg1

I use that a lot in function nodes.

[{"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"}}]

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.

Best shown with a few pictures.


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

(Sorry just passing by)

Searching the library (Library - Node-RED) 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?

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

1 Like

:rofl:

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:

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.

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.

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.

1 Like

The solution for me was

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

Thanks for your question about typing in node-red-contrib-ts! :rocket:

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

1) Explicit any type (straightforward approach)

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

2) Disable type checking (for legacy code)

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

3) Use the built-in MsgBase interface

4) The Msg interface can be customized

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

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

:light_bulb: 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

5 Likes