我是TypeScript的新手,我将把我的项目更改为TypeScript。但我犯了一些错误。我已经查过那些散布的论点,但我想不出正确的答案。
我已经试过了。
export const fetcher = async (...args) => {
return fetch(...args).then(async (res) => {
let payload;
try {
if (res.status === 204) return null; // 204 does not have body
p
我正在尝试向Typescript中的Array对象添加一个方法。我已经在SO上找到了其他解决方案,但这些方案都不适合我。
我的代码看起来像这样:
interface Array {
average(): () => number;
}
Array.prototype.average = () => {
var sum: number = 0
for (var i = 0; i < this.length; i++)
sum += this[i]
if (this.length)
return sum / t
我为我的应用程序conf.ts提供了一个配置文件,它将其他文件中的配置值合并起来,以保持它的组织性。我合并它们,因为当我想要使用一个配置值时,编写Conf.MY_LONG_NAMED_VALUE比编写Conf.SubCategory.MY_LONG_NAMED_VALUE更容易。
import {CoreConf} from './conf/game/core.conf';
import {ViewConf} from './conf/view/view.conf';
import {DebugConf} from './conf/game/debug.
我仍然很难使用TypeScript的类型检查系统。假设一个包含元素集合的组合,这些元素都是从一个公共基类派生的。如何实现递归上升层次结构并返回给定类型的第一个锚点的函数?
abstract class Employee
{
public Superior: Employee;
/** THIS IS NOT WORKING */
public getSuperiorOfType<T extends Employee>( type: typeof T ): T
{
if (this instanceof T) return this;
在TypeScript中,有没有办法键入以下fn函数,以便TypeScript仍然知道result的类型是'hello' const hello = 'hello' // hello is of type 'hello'
const fn = (x: unknown) => x
const result = fn(hello) // result is of type unknown 对于泛型,可以传递类型: const hello = 'hello' // hello is of type 'hello
我想了解为什么TypeScript不能推断以下函数的返回类型(虽然它能够在if-else语句中进行区分):
function calc (arg: number|string) {
if (typeof arg === 'number') {
// here typescript knows arg is number type
return arg
} else if (typeof arg === 'string') {
// here typescript knows arg is string type
retu
在TypeScript中使用async function扩展接口的好方法是什么?
向async function添加一个类似于常量的属性可能会很有用。
但是,我还没有能够将这种方法扩展到可以将AugmentedExample接口应用于多个不同的async function的方式。
,一个泛型被提供给接口,但是TypeScript抛出一个异常:An interface may only extend a class or another interface.这是可以理解的,因为TypeScript不能确定泛型是可以用来扩展接口的东西。然而,我还没有想出一种方法来约束泛型,这样TypeScript
我有一个函数URLBuilder,它作为一个模块工作(它返回一个包含各种函数的对象,这些函数与上下文对象绑定得很好,所以this绑定按预期工作,我们可以在这些函数中正确地访问this ) 我的问题是为什么typescript不理解URLBuilder的返回对象中函数的this绑定?尽管我尝试手动添加一个返回类型,但Typescript抱怨when using the URLBuilder在没有this上下文的情况下无法接受函数。 我们如何让typescript在返回的对象中理解这个绑定函数的this? 我要解决的Typescript错误是: The 'this' contex
在TypeScript 2.9.2中,我试图恢复函数声明的this类型。 这个是可能的吗? 这是我尝试过的: // Two different ways of specifying the type of `this`.
declare function foo(this: {hello: 'world'}): any;
type baz = ThisType<{hi: 'there'}> & (()=> any);
// Attempts to recover `typeof this` for foo and baz...
ty
我有这样一个实用程序函数,主要是为了避开typedef null是'object'的烦恼。
export function isObject(a: any): a is object {
return a && (typeof a === 'function' || typeof a === 'object');
}
与声明此函数boolean的返回类型不同,您可以使用a is object,它本质上也是一个boolean,但它有助于类型推断,以便TypeScript知道,如果该函数为真,则a可以被视为object,而无需进一
我使用GCC编译器(9.2.0版)
我想在C中使用typeof函数,但它会引发一个错误(错误:在“类型”之前的预期表达式)
如果你需要更多的信息,就问我。
int a = 5;
double b;
//the expected result is "0", but it raises an error instead...
printf("%d", typeof(a) == typeof(b));
给定的是函数的返回参数,它实际上是可调用的,尽管函数返回类型被定义为any。
const callback = target.getCallback();
但是当我想执行这个回调时,我得到了一个错误TS2349: This expression is not callable.
实例-守则:
callback(true);
我如何禁用TypeScript的错误,或者让TypeScript了解这个变量包含一个函数(这可能是这里最好的选择)?
使用在TypeScript 1.7中,正如我发现的,我们可以在返回类型为this的类中定义一个方法,并且自动地,任何扩展该类并继承这些方法的类都会将它们的返回类型设置为各自的this类型。就像这样:
class Model {
save():this { // return type: Model
// save the current instance and return it
}
}
class SomeModel extends Model {
// inherits the save() method - return type: SomeModel
}
我正在试着找出做这件事的最佳方法。我正在将一个库转换为typescript,并且遇到了一个我以前遇到过的特殊问题。有一个函数的定义看起来有点像这样
public execute(path: string | string[]): Promise<Object> {
if (typeof path == "string") {
// Turn the string into an array
}
}
问题是我不能将path参数转换为数组,因为它的类型是(string | string[])。尝试这样做也失败了。
public execute(path: