我正在类型记录中构建一个API,有些控制器操作可以是同步的,而另一些则不能。
type ActionResult =IHttpActionResult | Promise<IHttpActionResult>;
然后,当我构建操作时,当它们变得基于承诺时,我就可以预先准备异步并完成它。
但是,type typescript抱怨说,“异步函数或方法的返回类型必须是全局允诺类型。”
为什么异步函数不能返回T | Promise<T>的联合
下面是一个例子:
type StringPromise = Promise<string>;
// These two w
我不太理解TypeScript的Promise定义,如下所示: /**
* Represents the completion of an asynchronous operation
*/
interface Promise<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
*
我使用了带类型脚本的react。我使用了一个更高阶的组件来检查用户是否通过了身份验证。在添加hoc之后,我在路由中得到如下错误 /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/navigation/routes.ts
TypeScript error in /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/navigation/routes.ts(7,36):
Type 'Promise<typeof i
typescript类中的某个函数返回Promise<string>。如何展开/产生该promise中的值。
functionA(): Promise<string> {
// api call returns Promise<string>
}
functionB(): string {
return this.functionA() // how to unwrap the value inside this promise
}
我使用的是angularjs (1.5.8)框架和最新的typescript (2.8.0)文件。更新到最新的typescript版本后,以下代码未编译。
Angular接口实现:
interface IPromise<T> {
/**
* Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is availab
我在TypeScript 3.2.2和strict: true上。
我无法键入具有一组动态定义键的普通对象,但所有这些键都有一个有保证的非空值类型。
[TypeScript游乐场示例]()
// Will assume any property access has type T
interface Object<T> {
[key: string]: T;
}
// Will assume Object.values() could be undefined
interface Object<T> {
[key: string]: T | undef
Typescript@4.0.0rc9
在打字脚本编写定义中,用于手工编写的函数。当最终返回值为非promise时,它们可以工作,但当返回值为promise时,它们会失败。例如,这是可行的:
function test (
a: number,
): (
b: number,
) => number
function test (
a: number,
b: number,
): number
function test (a, b?) {
if (b === undefined)
return function (b: number) { test(a,
我需要使用TypeScript重载一个方法。
FooModel有6个参数,但两个字符串参数是唯一的强制参数。因此,我不想每次想要使用myMethod时都创建一个,而是重载myMethod,并在那里创建FooModel,然后在返回之前创建其余的逻辑。
我已经根据到目前为止在网上发现的内容尝试了这一点,但是我得到了以下错误:
TS2394: This overload signature is not compatible with its implementation signature.
此错误的解决方案与我的方法是不一致的。
static async myMethod(model:
在下面的代码中,我尝试创建一个访问给定输入的data属性的类型安全函数。我的目标是避免在任何地方重复对data的访问,为此,我希望将样板保持在最小(例如,我希望TypeScript推断尽可能多的类型)。
interface User {
name: string
}
interface HttpResponse<T> {
data: T
status: number
}
type HttpPromise<T> = Promise<HttpResponse<T>>
function fetch<T>(url
我有问题的任务中继格式Y轴只显示自然数。在项目中,我使用React a awith和Nivo Rocks库来创建图表(https://nivo.rocks/).for这个问题的目的我创建了沙箱: https://codesandbox.io/s/scuqn?file=/src/App.tsx 我找到了设置格式的解决方案: axisLeft={{
format: e => Math.floor(e) === e && e
}}
/> 但它只适用于纯js (没有类型检查)。对于TS,有一个类型错误(带有'e:number'): No o
我创建了一个通用的错误处理程序包装函数,用于在出现错误时处理错误,并返回回调的结果。我的问题是typescript不允许我返回承诺,因为 TS2322: Type 'Promise<unknown>' is not assignable to type 'R'.
'R' could be instantiated with an arbitrary type which could be unrelated to 'Promise<unknown>' 这就是我的功能 export function
我正在尝试使用typescript 2.8的新 (尚未发布2.8.0-dev.20180307版),我不知道这是错误还是误用。我的重点是我对MockedImplementation<F>的声明,它可以是一个与F完全匹配的函数,即F的返回类型,如果返回类型的F是一个Promise,那么它也可能是承诺所决定的--所有这些都将由mockIt()相应地包装。
type MockedImplementation<F> =
F | // The function signature
((ReturnType<F extends (...args: any[]