小编Mat*_*vic的帖子

为什么 ResizeObserver 构造函数回调接受条目数组而不是单个条目?

考虑到ResizeObserver 构造函数,其第一个参数是一个接受两个参数的回调,第一个参数是一个ResizeObserverEntry对象数组,这些条目之间有什么区别(如果有)?

javascript dom

9
推荐指数
1
解决办法
924
查看次数

在 Typescript 中创建一个元组数组

创建通用对类型(最简单的 Tuple )很容易,即

问题是如何创建一个类型来表示这种泛型对的数组。

唯一的要求是数组中的元素是一对。第一个元素的类型和第二个元素的类型应该是多态的,any不做切割,否则这将是令人满意的:

type Pair = [any, any]
type Pairs = Pair[]
Run Code Online (Sandbox Code Playgroud)

tuples typescript

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

React:更新子组件而不渲染父组件

以下是一个简单的示例:

const { Component } = React
const { render } = ReactDOM

const Label = ({text}) => (<p>{text}</p>)

const Clock = ({ date }) => (
  <div>{date.toLocaleTimeString()}</div>
)

class App extends Component {
  
  constructor() {
    super()
    this.state = {
      date: new Date()
    }
  }
  
  componentWillMount() {
    this.interval = setInterval(
      () => this.setState({ date: new Date() }),
      1000
    )
  }
  
  componentWillUnmount() {
    clearInterval(this.interval)
  }
  
  updateTime() {
    
  }
  
  render() {
    return (
      <div>
        <Label text="The current time is:" />
        <Clock date={this.state.date} …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

如何在TypeORM中使用订阅者?

考虑以下设置mongodb(问题可能与数据库无关):

实体/人物.ts

import { Entity, Column } from 'typeorm';

@Entity
export class Person {
  @Column()
  name: string;
}

Run Code Online (Sandbox Code Playgroud)

订阅者/PersonSubscriber.ts

import {
  EntitySubscriberInterface,
  EventSubscriber,
  InsertEvent,
} from 'typeorm';
import { Person } from '../entity/Person';

@EventSubscriber()
export class PersonSubscriber implements EntitySubscriberInterface<Person> {
  listenTo() {
    return Person;
  }

  afterInsert(event: InsertEvent<Person>) {
    console.log(event);
  }
}


Run Code Online (Sandbox Code Playgroud)

配置/mongo.ts

import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions';

export const mongoConfig: MongoConnectionOptions = {
  type: 'mongodb',
  host: 'localhost',
  database: process.env.DB_NAME,
  useNewUrlParser: true,
  useUnifiedTopology: true,
  synchronize: true,
  logging: false, …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js typeorm

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

F# 命令行交互式 Repl

我在 linux 上,我想对 F# 使用交互式命令行 repl。

比如像节点repl。

有哪些选择?

linux f#

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

如何在Typescript中按属性过滤对象类型的联合?

想象一下以下简化设置:

import { Action, AnyAction } from 'redux'; // interface Action<Type> { type: Type } and type AnyAction = Action<any>

export type FilterActionByType<
  A extends AnyAction,
  ActionType extends string
> = A['type'] extends ActionType ? A : never;

type ActionUnion = Action<'count/get'> | Action<'count/set'>;

type CountGetAction = FilterActionByType<ActionUnion, 'count/get'>;
// expected: Action<'count/get'>
// received: never

Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?(打字稿 3.7 是一个选项)

filter typescript redux union-types

4
推荐指数
2
解决办法
1778
查看次数

使用Math.max的Javascript Array.reduce

为什么第二个示例返回NaN,而第一个示例有效?

const numbers = [ 1, 2, 3 ]

console.log('passing arrow funciton', numbers.reduce((l, r) => Math.max(l, r)) ) // 3

console.log('passing bound function', numbers.reduce(Math.max.bind(Math)) ) // NaN
Run Code Online (Sandbox Code Playgroud)

为了给你一些上下文,reduce函数需要一个Callback参数.此外,回调需要两个参数,即累积和当前元素(您也可以左右调用它们等).有更多参数但它们是可选的.

我试图模仿reduce函数,这个例子就像魅力一样

const numbers = [1, 2, 3]

const reduce = (array, func) => {

  let largest = 0

  array.forEach
  (
    (number, i, a) =>
      a[i + 1]
      ? largest = func(number, a[i + 1])
      : console.log('done')
  )

  return largest

}

console.log( 'mock reduce', reduce(numbers, Math.max.bind(Math)) ) // 3
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

javascript arrays reduce max

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

标签 统计

javascript ×3

typescript ×2

arrays ×1

dom ×1

f# ×1

filter ×1

linux ×1

max ×1

mongodb ×1

node.js ×1

reactjs ×1

reduce ×1

redux ×1

tuples ×1

typeorm ×1

union-types ×1