小编Sir*_*hau的帖子

React + TypeScript:滚动事件类型不可分配给 window.addEventListener

难道事件的 TypeScript DOM 类型和 React 事件类型不能很好地配合吗?

看这段代码:

  useEffect(() => {
    const onScroll = (event: React.ChangeEvent<Body>) => {
      console.log("event", event.target);
    };

    window.addEventListener("scroll", onScroll);

    return () => window.removeEventListener("scroll", onScroll);
  }, []);
Run Code Online (Sandbox Code Playgroud)

它抛出

No overload matches this call.
  Overload 1 of 2, '(type: "scroll", listener: (this: Window, ev: Event) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
    Argument of type '(event: ChangeEvent<Body>) => void' is not assignable to parameter of type '(this: Window, ev: Event) => any'.
      Types …
Run Code Online (Sandbox Code Playgroud)

javascript events typescript reactjs

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

NodeJS:Typescript 编译器内存不足

我正在尝试在免费的 AWS EC 2 实例上运行节点服务器。我可以在本地构建并运行我的服务器,但在 AWS 上它总是崩溃。

我跑$ npm run build

错误信息:

findus    | <--- Last few GCs --->
findus    | 
findus    | [18:0x55b075815d80]    19411 ms: Mark-sweep 487.8 (491.0) -> 487.2 (491.2) MB, 721.0 / 0.0 ms  (average mu = 0.110, current mu = 0.019) allocation failure scavenge might not succeed
findus    | [18:0x55b075815d80]    20137 ms: Mark-sweep 487.9 (491.2) -> 487.3 (491.5) MB, 723.5 / 0.0 ms  (average mu = 0.060, current mu = 0.004) allocation failure scavenge might …
Run Code Online (Sandbox Code Playgroud)

memory heap-memory amazon-web-services node.js

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

Node + Docker Compose:开发和生产设置

我正在寻找一种使用 docker、docker-compose 和 nodejs 在我的项目中同时拥有开发生产环境的解决方案。

我该如何处理?

基本上我想要的是一个启动我的 docker 生产环境命令,以及一个启动我的开发环境命令(例如可以使用 nodemon)。

这是我的 Dockerfile

FROM node:13-alpine

RUN mkdir /app

WORKDIR /app

COPY . /app

RUN npm install

RUN npm run build

EXPOSE 1234

CMD ["npm", "run", "prod"] # <--- Have a possibility to run something like "npm run dev" here instead
Run Code Online (Sandbox Code Playgroud)

docker-compose.yml

version: "3"
services:
    findus:
        build: .
        ports:
            - "1234:1234"
        links:
            - mongo
        container_name: myapp
    mongo:
        image: mongo
        restart: …
Run Code Online (Sandbox Code Playgroud)

development-environment production-environment node.js docker docker-compose

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

Express + TypeScript:为response.locals 创建类型推断

我想为我的response.locals. 它用于将数据附加到请求-响应周期中。

我尝试过的

// ./types/express/index.d.ts
declare global {
    declare namespace Express {
        interface Response {
            locals: {
                userId: number;
            };
        }
    }
}

Run Code Online (Sandbox Code Playgroud)
// tsconfig.json
    "compilerOptions": {
        "typeRoots": ["./types"],
    }
Run Code Online (Sandbox Code Playgroud)
myController.post("/", async (request, response) => {
     // Can't get type in my controller
    const { userId } = response.locals; // <- userId is any
Run Code Online (Sandbox Code Playgroud)

目标:为我的response.locals变量获得正确的类型推断

版本: "express": "^4.17.1", "@types/express": "^4.17.8", "typescript": "^4.5.4"

node.js express typescript

6
推荐指数
2
解决办法
2739
查看次数

Typescript:如果设置了另一个可选类型,则需要设置类型

如果设置了第一种类型(本身是可选的),我想需要第二种类型。 有时我在代码中遇到问题,即设置一个可选属性后,还需要提供其他属性才能使反应组件正常工作。

例子

interface ComponentProps {
    title: string;
    isEnlarged?: boolean;
    text?: string; // <-- If isEnlarged is set, text shouldn't be optional
    imageUri?: string // <--- If isEnlarged is set, imageUri shouldn't be optional
}
Run Code Online (Sandbox Code Playgroud)

原因是通过类型检查强制组件行为一致。所有放大的组件都应包含文本和图像。

有没有办法通过 TypeScript 辅助函数或逻辑来实现这一点?

typescript

3
推荐指数
1
解决办法
1293
查看次数