使用扩展原型定义可调用的TypeScript函数(Define a callable TypeScript function with extended prototype)
我想定义一个可调用函数
foo()
(不是一个类,我不想在这里使用new
运算符),它也会有一个属性foo.bar()
。 在普通的JS中,它将如下所示:function foo() { // ... } foo.prototype.bar = function bar() { // ... }
我在TypeScript中使用这样的接口尝试了它:
interface IFoo { (): any; bar(): any; } const foo: IFoo = function(): any { // ... }; foo.prototype.bar = function bar(): any { // ... };
但是我得到一个错误:
error TS2322: Type '() => any' is not assignable to type 'IFoo'.
似乎TS抱怨定义
foo
和它的属性bar
之间的中间状态,即由于foo
还没有属性bar
它不能被分配给IFoo
类型的const
。 我怎样才能解决这个问题?以另一种方式提问:我如何为
IFoo
提供实施?I want to define a callable function
foo()
(not a class! I don't want to use thenew
operator here) that will also have a propertyfoo.bar()
. In plain JS it will look like this:function foo() { // ... } foo.prototype.bar = function bar() { // ... }
I tried it in TypeScript using an interface like this:
interface IFoo { (): any; bar(): any; } const foo: IFoo = function(): any { // ... }; foo.prototype.bar = function bar(): any { // ... };
But I get an error:
error TS2322: Type '() => any' is not assignable to type 'IFoo'.
It seems that TS complains about the intermediate state between defining
foo
and its propertybar
, that sincefoo
does not yet have a propertybar
it cannot be assigned to aconst
of typeIFoo
. How can I fix this?To ask it in another way: how can I provide an implementation for
IFoo
?
原文:https://stackoverflow.com/questions/43613387
最满意答案
您可以使用方法addEventListener(SerialPortEventListener侦听器,int掩码)。 只要你通过串口接收到一个字节,它就会调用回调方法。
字符串不完整的问题可能是两个问题
代码在收到整个字符串之前执行。 为了解决这个问题,你必须添加一个代码来验证你正在接收的字符串的长度。
您正在使用readString两次。 您可能会在第一次使用时丢失字符串的一些字节。
You could use the method addEventListener(SerialPortEventListener listener, int mask). It call a callback method whenever you receive a byte via serialPort.
The problem with the incomplete string could be 2 problems
The code is executing before the whole string is received. To solve this, you must add a code to verify the length of string you are receiving.
You are using readString twice. You may lost some bytes of the string in first usage.
相关问答
更多-
使用Java将变量发送到ubuntu上的Arduino COM端口(Sending variable to Arduino COM port on ubuntu using Java)[2023-02-03]
写入串口后尝试睡眠。 配置Arduino进行流量控制,首先看看正常的读写是否正常工作,然后移动到eventListener方法意味着如果java写入arduino,arduino能够读取,然后从arduino发送数据并且java端应该能够读取。 这将消除许多可能性。 进一步尝试一些其他类似的库也像SerialPundit或github上的fazecast。 Try sleeping after writing to serial port. Configure Arduino for flow contro ... -
import jssc.SerialPort; import jssc.SerialPortException; import jssc.SerialPortList; public class SerialPort1{ public static boolean sentBytes; //public SerialPort serialPort; public static void main(String[] args) { // get computer serial ...
-
Java JSSC rs232没有事件(Java JSSC rs232 no events)[2022-08-04]
问题是RTS和DTR线的默认设置是高,所以我需要使用另一个setParams方法,并根据设备的需要将RTS线设置为false。 解: serialPort.setParams(9600, 8, 1, 0, false, true); The problem was that the default setting of RTS and DTR line is High so i need to use another setParams method and set the RTS line to fal ... -
NullPointerException由null jssc.SerialPort引起。 通过替换修复 static SerialPort serialPort; 同 static SerialPort serialPort = new SerialPort(""); NullPointerException was caused by null jssc.SerialPort. Fixed by replacing static SerialPort serialPort; with static ...
-
用JAVA和jssc lib从Arduino串口读取字符串(Read String from Arduino serial port with JAVA and jssc lib)[2023-04-22]
您可以使用方法addEventListener(SerialPortEventListener侦听器,int掩码)。 只要你通过串口接收到一个字节,它就会调用回调方法。 字符串不完整的问题可能是两个问题 代码在收到整个字符串之前执行。 为了解决这个问题,你必须添加一个代码来验证你正在接收的字符串的长度。 您正在使用readString两次。 您可能会在第一次使用时丢失字符串的一些字节。 You could use the method addEventListener(SerialPortEventList ... -
目前不支持jssc,但您可以使用它编写自己的传输实现。 There is currently no support for jssc, but you could write your own transport implementation using it.
-
使用Java Simple Serial Connector将电脑与arduino连接起来(Connect pc with arduino using Java Simple Serial Connector)[2022-08-30]
我想,你的Arduinocode错了。 我这样做。 https://www.arduino.cc/en/Serial/Write Serial.write(val) Serial.write(str)Serial.write(buf,len) val:作为单个字节发送的值str:作为一系列字节发送的字符串buf:作为一系列字节发送的数组len:缓冲区的长度 I think, your Arduinocode is wrong. I do it like this. https://www.arduino. ... -
在linux中使用jSSC的串口(Serial port with jSSC in linux)[2023-06-15]
我最近遇到了同样的问题,这是我设法找到的。 您应该检查的第一件事是您是否有足够的权限访问tty *接口。 首先,确保您的用户处于dialout组。 如果没有,请执行 sudo adduser phil dialout 然后给自己读写确切端口的权限。 sudo chmod 666 /dev/ttyS0 之后, SerialPortList.getPortNames()应该返回ttyS0 I've faced the same issue recently, here's what I've managed ... -
Java和Arduino不能使用RXTX lib占用相同的COM端口?(Java and Arduino can't occupy the same COM port using RXTX lib?)[2023-12-29]
之后我没有关闭COM连接,所以它占用了端口。 我不得不在进程中手动关闭Java.exe以释放端口。 - 并写一个适当的关闭方法;) I didnt close the COM connection afterwards, so it occupied the port. I had to manually close the Java.exe in processes for the port to be freed. - and write a proper close method ;) -
您绑定双向值: tfAngle.textProperty().bindBidirectional(AngleSlider.valueProperty(), NumberFormat.getIntegerInstance()); 这意味着如果textfield或slider更改,则属性已更新 - 另一个组件会自动更新。 因此,像IncrBtnPress这样的方法IncrBtnPress应该只更新两个值: slider或textField 。 如果你想更新你的arduino的新脚踝:选择一个属性(例如你的te ...