在打字稿中定义全新类型的最佳做法?

时间:2018-01-01 23:29:11

标签: typescript types global-variables

我有一个全局有几个常量的执行环境:

import socket
import time
import threading

BUFF_SIZE = 1024
HOST = str(input("Enter Server IP: "))
PORT = 9999

print("Connecting to {} at port {}.\n".format(HOST, PORT))

lock = threading.Lock()

USERNAME = str(input("Enter username: "))

print("""\nClientCommands:
-----------------
\t1. @SHOWINFO --> Shows server info.
\t2. @ARCIVECHAT --> Saves chat into text file.
""")

class Client:
    def __init__(self):
        self.messages = []
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = HOST
        self.port = PORT
        self.sock.connect((self.host, self.port))
        print("\nConnected to server!\n")
        print("Please press <enter> to refresh chat in order to send message.")
        _recver = threading.Thread(target=self.recver)
        _recver.start()
    def recver(self):
        while True:
            with lock:
                data = self.sock.recv(BUFF_SIZE).decode("utf-8")
                print(data)
                self.messages.append(data)
                data = self.sock.recv(BUFF_SIZE).decode("utf-8")
                print(data)
                self.messages.append(data)
                time.sleep(.01)
    def get_send_msg(self):
        self._msg_to_send = str(input("You - "))
        self.msg_to_send = "{} - ".format(USERNAME)+self._msg_to_send
        self.messages.append(self.msg_to_send)
        try:
            if self._msg_to_send == "@SHOWINFO":
                self.ShowInfo()
            elif self._msg_to_send == "@ARCIVECHAT":
                self.Arcive_Chat()
            else:
                self.sock.send(self.msg_to_send.encode("utf-8"))
        except Exception as e:
            print("An error has occured:")
            print(e)
            archiveornot = str(input("Would you like to archive the conversation in a text file(Y/N): "))
            if archiveornot == "Y":
                archive_name = str(input("Enter file name: "))
                f = open(archive_name, "w")
                for message in self.messages:
                    f.write("\n")
                    f.write(message)
                f.close()
                print("Exiting program...")
                quit()
            else:
                print("Exiting program...")
                time.sleep(2)
                quit()
        time.sleep(.01)
    def ShowInfo(self):
        print("Server Info:")
        print("--------")
        print("Host: {} | Port: {}".format(self.host, self.port))
        print("--------")
    def Arcive_Chat(self):
        archive_name = str(input("Enter file name: "))
        f = open(archive_name, "w")
        if len(self.messages) >= 1:
            for message in self.messages:
                f.write("\n")
                f.write(message)
            f.close()
        else:
            print("[!]Error, No messages were sent[!]")


client = Client()

def main():
    while True:
        client.get_send_msg()

if __name__ == "__main__":
    main()

我很清楚,这8个常数不仅仅是任意常数,而且实际上是一组方向。因此,我希望它们属于TOP = 1 TOP_RIGHT = 2 RIGHT = 3 BOTTOM_RIGHT = 4 BOTTOM = 5 BOTTOM_LEFT = 6 LEFT = 7 TOP_LEFT = 8 类型,以便我可以做到这样的事情:

DIRECTION

我已经考虑了如何实现这一目标的几个选项(即放入let test: DIRECTION = TOP_LEFT; let foo: DIRECTION = TOP; let bar: DIRECTION = LEFT; let target: DIRECTION = RIGHT; 文件中的内容):

  1. direction.d.ts需要大量语法&amp;在这种情况下它们是不受欢迎的样板。
  2. 当我使用enum关键字时,似乎无法创建一个不仅仅是另一个别名的新类型。 (例如,type是允许的,但type DIRECTION=integer;不是。将DIRECTION定义为整数的别名是不可取的,因为尽管它们在技术上是环境中的整数,type DIRECTION;是荒谬的,并且应该导致错误。
  3. BOTTOM-2似乎会产生所需的行为,让我定义这种新类型。然而,这似乎是对打字系统的滥用(我认为TS的界面基本上等同于类。)
  4. 最友好的TS方式是什么?

3 个答案:

答案 0 :(得分:0)

您可以使用consttype

const TOP = 1
const TOP_RIGHT = 2
type DIRECTION = 1 | 2

let x: DIRECTION = TOP

但是,我相信enum是一个更好的解决方案。

例如,这是有效但不可取的:

const ONE = 1
let x: DIRECTION = ONE

答案 1 :(得分:0)

参考解决方案#1,最初发布:

declare const enum Direction {
    TOP = 1,
    TOP_RIGHT = 2,
    RIGHT = 3,
    BOTTOM_RIGHT = 4,
    BOTTOM = 5,
    BOTTOM_LEFT = 6,
    LEFT = 7,
    TOP_LEFT = 8
}
declare const TOP=Direction.TOP
declare const TOP_RIGHT=Direction.TOP_RIGHT
declare const RIGHT=Direction.RIGHT
declare const BOTTOM_RIGHT=Direction.BOTTOM_RIGHT
declare const BOTTOM=Direction.BOTTOM
declare const BOTTOM_LEFT=Direction.BOTTOM_LEFT
declare const LEFT=Direction.LEFT
declare const TOP_LEFT=Direction.TOP_LEFT

答案 2 :(得分:0)

参考想法#3,如上所述:

interface DIRECTION {}

declare const TOP: DIRECTION;
declare const TOP_RIGHT: DIRECTION;
declare const RIGHT: DIRECTION;
declare const BOTTOM_RIGHT: DIRECTION;
declare const BOTTOM: DIRECTION;
declare const BOTTOM_LEFT: DIRECTION;
declare const LEFT: DIRECTION;
declare const TOP_LEFT: DIRECTION;

此代码比使用enum的解决方案简洁得多。

我仍然在寻找改进它的方法,所以欢迎提出建议/改变。