考虑到ResizeObserver 构造函数,其第一个参数是一个接受两个参数的回调,第一个参数是一个ResizeObserverEntry对象数组,这些条目之间有什么区别(如果有)?
创建通用对类型(最简单的 Tuple )很容易,即
type Pair<A, B> = [A, B]Run Code Online (Sandbox Code Playgroud)
问题是如何创建一个类型来表示这种泛型对的数组。
唯一的要求是数组中的元素是一对。第一个元素的类型和第二个元素的类型应该是多态的,any不做切割,否则这将是令人满意的:
type Pair = [any, any]
type Pairs = Pair[]Run Code Online (Sandbox Code Playgroud)
以下是一个简单的示例:
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)考虑以下设置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) 想象一下以下简化设置:
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 是一个选项)
为什么第二个示例返回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)) ) // NaNRun 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)) ) // 3Run Code Online (Sandbox Code Playgroud)