我目前正在尝试从 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) 有没有办法禁用 Chrome 的内存缓存而不更改响应中的缓存标头?
我找到了一些关于禁用缓存、缓存标头、CLI 参数(如--disk-cache-size=1和其他一些参数)的答案,但它们似乎都无法禁用内存缓存。使用该--disk-cache-size=1标志,我能够禁用磁盘缓存,但我找不到对内存缓存执行相同操作的方法。
在我的场景中,我运行自动化测试,加载带有请求的页面,该请求通过缓存标头进行响应。但是,我希望每次测试运行时都加载此资源,即使浏览器未重新启动(性能原因),以便正确模拟延迟的页面加载。我无法以任何方式更改请求或响应,因此我必须更改浏览器加载它的方式。
对于第一次测试运行,由于 ,这工作正常--disk-cache-size=1,但后续运行不起作用,因为 Chrome 从内存加载资源。
我既无法更改请求,也无法更改响应,因此无法更改缓存标头或向资源的 URL 附加某些内容。我想以自动化方式运行这些测试,因此在开发工具中禁用缓存也不起作用。因此浏览器扩展也是不可行的。
有没有其他方法可以禁用 Chrome(或其他基于 chromium 的浏览器)中的内存缓存?
为什么测试 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)
这是我的问题:
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) 我的通用函数之一中的可分配值有问题:
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)