小编Trí*_*han的帖子

Typescript:需要至少一个属性的类型

考虑我有以下类型:

type SomeType = {
  propOne: any;
  propTwo: any;
  propThree: any;
}
Run Code Online (Sandbox Code Playgroud)

propOne必需的,propTwo并且propThree是可选的,但至少其中之一是必需的。如何定义具有该约束的类型?

// The following code is my expectation
let someVar1: SomeType = { propOne: 1, propTwo: "two" } //Okay
let someVar2: SomeType = { propOne: 1, propThree: "three" } //Okay
let someVar3: SomeType = { propOne: 1, propTwo: "two", propThree: "three" } //Okay
let someVar4: SomeType = { propOne: 1 } //Not Okay
Run Code Online (Sandbox Code Playgroud)

javascript typescript

8
推荐指数
1
解决办法
1万
查看次数

React Hooks - useReducer:在触发函数之前等待减速器完成

我有使用useReducerHooks的组件:

const init = {
  statA: true,
  statB: true
};

const reducer = (state, action) => {
  switch (action.type) {
    case "ActionA":
      return { ...state, statA: !state.statA };
    case "ActionB":
      return { ...state, statB: !state.statB };
    default:
      return state;
  }
};

const App = () => {
  const [state, dispatch] = useReducer(reducer, init);

  const clickMe = () => {
    dispatch({ type: "ActionA" });
    dispatch({ type: "ActionB" });
    console.log(state);
  }

  return(
      <button onClick={() => clickMe()}>Click Me</button>
  );
};
Run Code Online (Sandbox Code Playgroud)

单击按钮时,状态将更改。但是当我查看日志时,它会打印之前的状态,而不是当前更新的状态。 …

javascript reactjs react-hooks

7
推荐指数
1
解决办法
6810
查看次数

使用 TypeScript Enum 和 PropTypes 来定义 React 组件道具

我有组件,我使用 TypeScript 接口来定义它的道具:

interface Props {
    headerType: DROPDOWN_MENU_TYPE, //DROPDOWN_MENU_TYPE is enum
    headerContent: SVGClass
    isReverse: boolean;
};

const MyComp: React.FunctionComponent<Props> = () => {};
Run Code Online (Sandbox Code Playgroud)

然后我使用 PropTypes 在运行时验证它的 props:

DropDownMenu.propTypes = {
    headerType: PropTypes.oneOf(DROPDOWN_MENU_TYPE), //error here
    headerContent: PropTypes.instanceOf(SVGClass),
    isReverse: PropTypes.bool
};
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

“typeof DROPDOWN_MENU_TYPE”类型的参数不能分配给“readonly DROPDOWN_MENU_TYPE[]”类型的参数。

类型 'typeof DROPDOWN_MENU_TYPE' 缺少类型 'readonly DROPDOWN_MENU_TYPE[]' 中的以下属性:长度、连接、连接、切片和其他 16 个。

如何将 TypeScriptenumPropTypes? 我该如何解决这个错误?

javascript typescript reactjs

6
推荐指数
1
解决办法
1174
查看次数

ReactJS中`useRef`和ref变量的区别

我有这个

const CompA = () => {
  let _input;
  return (
    <input ref={el => _input = el} type="text" />
  );
}
Run Code Online (Sandbox Code Playgroud)

和这个

const CompB = () => {
  const _input = useRef(null);
  return (
    <input ref={_input} type="text" />
  );
}
Run Code Online (Sandbox Code Playgroud)

_inputinput标签的 ref 对象,我找不到它们之间的差异。

我的问题是:两者之间有什么区别_input,哪个_input更好?

javascript reactjs react-ref react-hooks

5
推荐指数
1
解决办法
2350
查看次数

如何在 Visual Studio Code 中重命名我的 a.exe 文件?

我有一个main.cpp文件,当我使用 构建它时task.json,它会生成一个a.exe文件。如何在构建文件时将该exe文件的名称更改为其他名称?main.execpp

c++ visual-studio-code

4
推荐指数
1
解决办法
1414
查看次数

Typescript: (intermediate value).(...) 从派生类调用父类的方法时不是函数

这是我的父类,它具有trigger方法,即public方法:

class BaseEffect {
    //properties and contructor...
    //other methods...

    public trigger = (): void => void (this.target.hitPoint -= this.amount);
}
Run Code Online (Sandbox Code Playgroud)

和一个TurnBasedEffect扩展BaseEffect类:

class TurnBasedEffect extends BaseEffect {
    //properties and contructor...
    //other methods...

    public trigger = (): void => {
        super.trigger();
        this.remainingTurns--;
    };
}
Run Code Online (Sandbox Code Playgroud)

这个类也有trigger方法,在这个方法里面,trigger调用了父类的一个方法。

问题是当trigger调用派生类的方法时,打字稿会抛出此错误:

TypeError: (intermediate value).trigger is not a function
Run Code Online (Sandbox Code Playgroud)

并指向该类trigger方法的这一行TurnBasedEffect

//...
super.trigger();
//...
Run Code Online (Sandbox Code Playgroud)

我的课程有什么问题以及如何解决这个问题?

javascript class super typescript

2
推荐指数
1
解决办法
553
查看次数

套接字 IO 发出两次事件

情况:

我正在使用 socket.io 设置快速服务器和使用 ReactJS 的客户端。这是我的服务器:

//... another requirement
const socket = require("./socket");

const app = express();
const server = http.createServer(app);
const port = process.env.PORT || 3000;

//... some parsers ...

routes(app);
socket(server);

server.listen(port);
console.log("Server started on: " + port);
Run Code Online (Sandbox Code Playgroud)

这是我的插座:

const socketIO = require("socket.io");

const socket = server => {
  const io = socketIO(server);
  const chat = io.of("/chat");
  chat.on("connection", client => {
    console.log("User started chat!");
    client.on("disconnect", () => {
      console.log("User left chat!");
    });

    client.on("sendMessage", msg => {
      const { …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express socket.io reactjs

0
推荐指数
1
解决办法
5732
查看次数

模块中的私有方法和普通函数

考虑到我有以下模块:

class Rectangle {
  private width: number;
  private height: number;

  constructor(w: number, h: number) {
    this.width = w;
    this.height = h;
  }

  private area(): number {
    return w * h;
  }

  public show(): void {
    console.log(`${w} X ${h} = ${this.area()}`);
  }
}

export default Rectangle;
Run Code Online (Sandbox Code Playgroud)

如您所见,该area方法设置为private,因此我可以将其移出类并将其转换为普通函数。因为我不导出它,无法从外部代码访问它。

const area = (w: number, h: number): number => w * h;

class Rectangle {
  private width: number;
  private height: number;

  constructor(w: number, h: number) {
    this.width = w;
    this.height …
Run Code Online (Sandbox Code Playgroud)

javascript oop methods performance typescript

0
推荐指数
1
解决办法
39
查看次数