假设我有一个函数类型接口,我想用它来强制执行特定函数的某个签名。
有没有办法强制函数的最后一个参数必须是特定类型?
有没有办法做这样的事情:
interface SomeType { /*...*/ }
/**
* this is the function type I want to enforce
* I want the last argument of any `Modifcation` to be `SomeType`
*/
interface Modification {
(...args: [...firstArgs: any[], lastArgument: SomeType]): void
}
// enforcing `Modification`
const someModification: Modification = (firstParam: string, second: number, third: SomeType) => {
/*...*/
}
Run Code Online (Sandbox Code Playgroud)
我知道打字稿使用传播语法和元组类型,但上面的伪代码不起作用。
有没有办法做到这一点?
编辑:
免责声明:我认为没有办法做我想做的事情,因此这个问题可能不完全具有建设性。
语境:
我有一组类,我想对这些类的特定方法强制执行某种约定。我可以通过声明另一个接口并说明我的类实现该接口来强制某个方法必须符合一个函数类型。
例如
interface Convention {(arg0: string, …Run Code Online (Sandbox Code Playgroud) React 文档表明您可以通过将 s 包装在from 中来延迟setStatesstartTransitionuseTransition。
我想知道是否有人知道这是如何工作的以及有哪些限制。
特别是我的问题是:
startTransition吗?dispatch中startTransition?isPending的useTransition时候像终极版的setStatesdispatch考虑?所以我使用bundlephobia来审核我的包的包大小。Bundlephobia 有一个名为“导出分析”的部分,它为您提供各个导出的包大小。
因此,这个包中的每个函数都是纯函数,因此,除非我遗漏了某些东西,否则我认为每个函数应该显示较小的大小,但所有函数(除了重新导出的函数)都具有相同的大小。
我试过:
"sideEffects": false"到我的package.json#__PURE__我正在使用汇总,这是我的汇总配置:
// rollup.config.js
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
const extensions = ['.js', '.ts', '.tsx'];
export default [
{
input: './src/index.ts',
plugins: [
resolve({
extensions,
}),
babel({
babelrc: false,
presets: ['@babel/preset-env', '@babel/preset-typescript'],
babelHelpers: 'bundled',
extensions,
}),
],
output: {
file: 'dist/index.js',
format: 'umd',
name: 'parseToRgba',
sourcemap: true,
},
},
{
input: './src/index.ts',
plugins: [
resolve({
extensions,
modulesOnly: true,
}),
babel({
babelrc: false,
presets: …Run Code Online (Sandbox Code Playgroud) 所以我知道React中的设置状态是异步的,并且全部,我们这样设置状态:
this.setState(previousState => {
return { /* some new state that uses `previousState` */ }
});
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如何取消this.setState?假设我previousState用来确定不需要渲染更新。我该如何取消setState并告诉React不要重新渲染任何东西。
this.setState(previousState => {
if (/* previousState is fine */) {
// tell react not to do anything
} else {
return { /* some new state */ }
}
});
Run Code Online (Sandbox Code Playgroud) 我正在寻找创建一个像lodash 的 get一样工作但需要输入的函数。
这是我到目前为止所拥有的:
function safeGet<T, R>(subject: T, select: (t: NonNullable<T>) => R): R | undefined {
try { return select(subject); }
catch { return undefined; }
}
// example use
interface MyType {
someKey: {
someValue: string | undefined;
} | undefined
}
let exampleSubject: MyType = /* ... */;
const gotSomeValue = safeGet(exampleSubject, s => s.someKey.someValue)
Run Code Online (Sandbox Code Playgroud)
这种方法可行,但它不会递归删除未定义的内容。有没有办法创建某种递归NonNullable类型?
我正在尝试编写一个用户定义的类型保护来测试给定的值是否具有给定数组中的所有属性。
我调用这个函数hasAll,它在 Javascript 中的实现和用法如下所示:
function hasAll(obj, keysToCheck) {
if (!obj) return false;
for (const key of keysToCheck) {
const value = obj[key];
if (value === null) return false;
if (value === undefined) return false;
}
return true;
}
hasAll({ foo: 'test', bar: 5 }, ['foo', 'bar']); // true
hasAll({ foo: 'test', bar: 5 }, ['foo', 'bar', 'baz']); // false
Run Code Online (Sandbox Code Playgroud)
我现在想做的是将上面的函数变成类型保护。这是我到目前为止所拥有的:
// this _almost_ works
type Nullable<T> = T | null | undefined;
type RemoveNullables<T, …Run Code Online (Sandbox Code Playgroud) 在打字稿中,可以使用泛型添加属性键吗?
function f<T extends string>(k: T) {
return { [k]: 'test'; };
}
const obj = f('foo');
// some how assert that obj.foo exists
Run Code Online (Sandbox Code Playgroud)
我有一个类似于上面的函数,它接收一个键k并使用{[identifier]: 'value'}.
我想知道是否可以捕获字符串文字类型,例如'some-key'/T extends string并在另一种类型中使用文字。像这样的东西:
interface F<T extends string> {
[T]: SomeRandomType;
otherKey: string;
anotherKey: number;
}
interface SomeRandomType { /* ... */ }
const f: F<'bar'> = /* ... */;
f.otherKey; // should work
f.anotherKey; // should work
f.bar; // should work
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?这不可能吗?
所以我有很多这样的类型:
interface ReadyMessage {
type: 'ready';
}
interface ProgressMessage {
type: 'progress';
message: string;
}
interface DoneMessage {
type: 'done';
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个这种类型的联合,如下所示:
type Message = ReadyMessage | ProgressMessage | DoneMessage;
Run Code Online (Sandbox Code Playgroud)
如何创建一个记录,其中键是Message['type'],值是相应的类型。
IE
type Matchers = {
ready: { type: 'ready'; };
progress: { type: 'progress'; message: string; };
done: { type: 'done'; }
}
Run Code Online (Sandbox Code Playgroud)
做类似的事情type Matchers = Record<Message['type'], Message>但这会导致值类型变得Message更具体ProgressMessage。
有任何想法吗?