小编Ger*_*ner的帖子

为什么打字稿在通过泛型检索函数类型时期望“从不”作为函数参数?

我目前正在尝试从 TS 2.6 切换到 3.4,但遇到了奇怪的问题。这以前有效,但现在它向我展示了一个编译器错误:

type MyNumberType = 'never' | 'gonna';
type MyStringType = 'give' | 'you';
type MyBooleanType = 'up' 

interface Bar {
    foo(key: MyNumberType): number;
    bar(key: MyStringType): string;
    baz(key: MyBooleanType): boolean;
}

function test<T extends keyof Bar>(bar: Bar, fn: T) {
    let arg: Parameters<Bar[T]>[0];
    bar[fn](arg); // error here
}
Run Code Online (Sandbox Code Playgroud)

错误如下:

Argument of type 'Parameters<Bar[T]>' is not assignable to parameter of type 'never'.
Type 'unknown[]' is not assignable to type 'never'.
Type '[number] | [string] | [boolean]' is not …
Run Code Online (Sandbox Code Playgroud)

typescript

11
推荐指数
1
解决办法
1650
查看次数

如何在没有开发工具的情况下禁用 Chrome 的内存缓存?

有没有办法禁用 Chrome 的内存缓存而不更改响应中的缓存标头?

我找到了一些关于禁用缓存、缓存标头、CLI 参数(如--disk-cache-size=1和其他一些参数)的答案,但它们似乎都无法禁用内存缓存。使用该--disk-cache-size=1标志,我能够禁用磁盘缓存,但我找不到对内存缓存执行相同操作的方法。

在我的场景中,我运行自动化测试,加载带有请求的页面,该请求通过缓存标头进行响应。但是,我希望每次测试运行时都加载此资源,即使浏览器未重新启动(性能原因),以便正确模拟延迟的页面加载。我无法以任何方式更改请求或响应,因此我必须更改浏览器加载它的方式。

对于第一次测试运行,由于 ,这工作正常--disk-cache-size=1,但后续运行不起作用,因为 Chrome 从内存加载资源。

我既无法更改请求,也无法更改响应,因此无法更改缓存标头或向资源的 URL 附加某些内容。我想以自动化方式运行这些测试,因此在开发工具中禁用缓存也不起作用。因此浏览器扩展也是不可行的。

有没有其他方法可以禁用 Chrome(或其他基于 chromium 的浏览器)中的内存缓存?

caching google-chrome chromium

6
推荐指数
0
解决办法
1841
查看次数

使用括号表示法访问对象属性时的类型缩小中断

为什么测试 1 和 2 在这里可以工作,但测试 3 显示编译器错误foo[barConst]++:“对象可能是“未定义”。”?我经常需要通过括号表示法访问属性,因此喜欢为这些属性添加常量,但 TypeScript 不允许这样做。它也不适用于const enums。这是一个错误还是有一个很好的原因导致该错误?

const barConst = 'bar';

interface Foo {
    [barConst]?: number;
}

function test1(foo?: Foo) {
    if (foo && foo.bar) {
        foo.bar++;
    }
}

function test2(foo?: Foo) {
    if (foo && foo['bar']) {
        foo['bar']++;
    }
}

function test3(foo?: Foo) {
    if (foo && foo[barConst]) {
        foo[barConst]++; // compiler error: 'Object is possibly "undefined".'
    }
}
Run Code Online (Sandbox Code Playgroud)

操场

properties typescript type-narrowing

5
推荐指数
1
解决办法
1748
查看次数

为什么打字稿不能正确推断条件的泛型类型?

这是我的问题:

export type ForEachList<T> =
    T extends Element ? (NodeListOf<T> | HTMLCollectionOf<T>) :
    T extends CSSRule ? CSSRuleList :
    T extends Attr ? NamedNodeMap :
    T extends Node ? (Node[] | NodeList) :
    T[];

export function forEach<T>(array: ForEachList<T>,
                        forEachFn: (value: T, index: number, arr: ForEachList<T>, done: () => void) => boolean | void,
                        thisArg?: any): void { 
    for (let i = 0; i < array.length; i++) {
        const foo = array[i];
        forEachFn.call(thisArg, foo, i, array, () => { /* empty …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
0
解决办法
59
查看次数

TypeScript:条件泛型类型作为函数返回值不可分配

我的通用函数之一中的可分配值有问题:

interface BuildArguments<T extends string> {
    type: T;
}

type PromiseResult<T> =
    T extends 'standalone' ? Promise<void> :
    T extends 'all' ? Promise<void> :
    Promise<void[]>;    

const foo: PromiseResult<'standalone'> = Promise.resolve();
const bar: PromiseResult<'all'> = Promise.resolve();
const baz: PromiseResult<'foo'> = Promise.resolve([]);

bundle({ type: 'foo' });

function bundle<T extends string>(buildArguments: BuildArguments<T>): PromiseResult<T> {
    switch (buildArguments.type) {
        case 'standalone':
            return Promise.resolve(); // error here, not assignable to PromiseResult<T>
        case 'all':
            return Promise.resolve(); // error here, not assignable to PromiseResult<T>
        default:
            return Promise.all([ // …
Run Code Online (Sandbox Code Playgroud)

typescript

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