小编Lio*_*Tay的帖子

打字稿:将函数的参数限制为与特定类型值关联的对象的键

有没有办法进行以下类型检查?

function getNumberFromObject<T>(obj: T, key: keyof T): number {
  return obj[key] // ERROR: obj[key] might not be a number
}
Run Code Online (Sandbox Code Playgroud)

我想指定它key不仅应该是 的键T,而且应该是带有number值的键。

generics typescript keyof

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

GraphQL Dataloader事先不知道键

Dataloader能够批处理和缓存请求,但只能通过调用load(key)或loadMany(keys)来使用。

我遇到的问题是,有时我不知道它们是我要预先加载的项目的键。

我使用的是sql数据库,当当前对象具有与另一个模型的belongsTo关系中的外键时,此方法工作正常。

例如,一个用户属于一个组,因此具有一个groupId。要解析该组,您只需调用groupLoader.load(groupId)。

另一方面,如果我想解析一个组中的用户,那么其中可能会有很多我想要一个查询,例如

SELECT * from users where user.groupId = theParticularGroupId
Run Code Online (Sandbox Code Playgroud)

但是这样的查询不使用用户的键,因此我不确定如何使用数据加载器。

我可以再次请求以获取诸如

SELECT id from users where user.groupId = theParticularGroupId
Run Code Online (Sandbox Code Playgroud)

然后使用这些键调用loadMany ...但是我可以直接直接请求数据。

我注意到,数据加载器具有一个prime(key,value)函数,该函数可用于启动缓存,但是只有在数据已被获取后才能执行。届时许多查询将已经发送,并且重复的数据可能已经被获取。


另一个示例将是以下查询

query {
  groups(limit: 10) {
    id
    ...
    users {
      id
      name
      ...
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我要搜索前10个组或最后10个组,则无法知道按键。然后,一旦我有这10个小组。我不知道他们用户的密钥,以及每个解析器是否会使用查询来解析用户,例如

SELECT * from users where user.groupId = theParticularGroupId
Run Code Online (Sandbox Code Playgroud)

该查询将被执行10次。加载数据后,我现在可以准备缓存,但是已经发出了10个请求。

有什么办法解决这个问题?也许不同的模式或数据库结构,或者数据加载器甚至都不是正确的解决方案。

javascript caching batching node.js graphql

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

Typescript React:基于另一个道具的类型的条件可选道具

我正在尝试输入正在使用的提取程序组件API。这个想法很简单,给它一个fetcher(承诺返回函数)和一个params数组(代表位置参数)作为道具,它将结果提供给渲染道具。

type FunctionType<A extends any[] = any[], R = any> = (...args: A) => R

type Arguments<T> = T extends FunctionType<infer R, any> ? R : never

type PromiseReturnType<T> = T extends (...args: any[]) => Promise<infer R>
  ? R
  : never

type ChildrenProps<F> = {
  data: PromiseReturnType<F>
}

type Props<F> = {
  fetcher: F
  params: Arguments<F>
  children: (props: ChildrenProps<F>) => React.ReactNode
}

class Fetch<F> extends React.Component<Props<F>> {
  render() {
    return null
  }
}

const usage = …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

Typescript React:提供多余属性时,道具的联合类型不会显示错误

我正在尝试对 React 组件的 props 使用联合类型

type Props =
  | {
      type: "string"
      value: string
    }
  | {
      type: "number"
      value: number
    }
  | {
      type: "none"
    }

class DynamicProps extends React.Component<Props> {
  render() {
    return null
  }
}

// Ok
const string_jsx = <DynamicProps type="string" value="hello" />

// Error as expected, value should be a string
const string_jsx_bad = <DynamicProps type="string" value={5} />

// Ok
const number_jsx = <DynamicProps type="number" value={5} />

// Error as expcted value should be a …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

打字稿:输入对象具有接口键的子集

我希望能够指定函数的输入对象应仅包含现有接口中存在的键(它不需要具有所有键)。

interface Group {
  name: string
}

interface Person {
  id: number
  name: string
  email: string
  age: number
}

interface AddedProperties {
  groups: Group[]
}

function createPerson<D extends object>( // <-- Should convey, D is an object with some keys of Person
  personData: D
): D & AddedProperties {
  return Object.assign({}, personData, { groups: [] })
}

interface NameAndAge {
  name: string
  age: number
}

// Valid: Should be valid
const person: NameAndAge = createPerson({ name: "bob", age: 5 }) …
Run Code Online (Sandbox Code Playgroud)

types typescript

3
推荐指数
2
解决办法
2449
查看次数

打字稿:重载函数的ReturnType

给出的类型ReturnType似乎取决于重载签名的写入顺序

function applyChanges1(input: string): number
function applyChanges1(input: number): string
function applyChanges1(input: number | string): number | string {
  return typeof input === "number" ? input.toString() : input.length
}

function applyChanges2(input: number): string
function applyChanges2(input: string): number
function applyChanges2(input: number | string): number | string {
  return typeof input === "number" ? input.toString() : input.length
}

type Ret1 = ReturnType<typeof applyChanges1> // string
type Ret2 = ReturnType<typeof applyChanges2> // number
Run Code Online (Sandbox Code Playgroud)

似乎采用了最后一个重载签名的返回类型,这似乎很随意。我期待Ret1Ret2都会string | number。有这种行为的原因吗?

typescript

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

打字稿:映射的键与值的类型相同,而不限制键的类型

有没有办法进行以下类型检查?

const map = new Map()

map.set('stringkey', 'stringvalue') // OK
map.set(5, 10) // OK
const objectKey = { name: 'bob' }
map.set(objectKey, { name: 'alice' }) // OK

const stringValue: string = map.get('stringkey') // OK, but is typed as 'any'
const objectValue: { name: string } = map.get(objectKey) // OK, but is typed as 'any'

map.set('string', 5) // Error
map.set({ name: 'bob' }, { food: 'cake' }) // Error
Run Code Online (Sandbox Code Playgroud)

使用默认值

const map = new Map<any, any>()
Run Code Online (Sandbox Code Playgroud)

有效,但在访问值时没有提供有用的类型,因为它们都被键入为 any。

generics dictionary typescript

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

Typescript:具有排除更改属性修饰符的映射类型

当使用 Minus 映射类型时,它似乎从属性中删除修饰符。我认为这是由排除类型引起的,但我不确定为什么。

我期望 Minus 只从 T 中删除 U 的键,而不更改 T 的属性修饰符。

type Minus<T, U> = { [P in Exclude<keyof T, keyof U>]: T[P] }
type Noop<T> = { [P in keyof T]: T[P] }

interface Student {
  readonly gpa: number
  hobby?: string
  name: string
}

interface Person {
  name: string
}

type Difference = Minus<Student, Person>
// type Difference = {
//   gpa: number; <-- Where did readonly go?
//   hobby: string | undefined; <-- Why is it no …
Run Code Online (Sandbox Code Playgroud)

modifier typescript mapped-types

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