考虑我有以下类型:
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) 我有使用useReducer
Hooks的组件:
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)
单击按钮时,状态将更改。但是当我查看日志时,它会打印之前的状态,而不是当前更新的状态。 …
我有组件,我使用 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 个。
如何将 TypeScriptenum
与PropTypes
? 我该如何解决这个错误?
我有这个
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)
二_input
是input
标签的 ref 对象,我找不到它们之间的差异。
我的问题是:两者之间有什么区别_input
,哪个_input
更好?
我有一个main.cpp
文件,当我使用 构建它时task.json
,它会生成一个a.exe
文件。如何在构建文件时将该exe
文件的名称更改为其他名称?main.exe
cpp
这是我的父类,它具有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)
我的课程有什么问题以及如何解决这个问题?
情况:
我正在使用 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) 考虑到我有以下模块:
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 ×7
reactjs ×4
typescript ×4
react-hooks ×2
c++ ×1
class ×1
express ×1
methods ×1
node.js ×1
oop ×1
performance ×1
react-ref ×1
socket.io ×1
super ×1