尝试编写一个基本的减速器以从对象数组中返回键(的值)数组。某些键可能丢失或未定义。
我的代码:
const data = [{Key: 56}, {Key: undefined}, {}, {Key: 44}]
const keys = data.reduce((prev, curr) => {
if (curr.Key) {
return [...prev, curr.Key];
} else return prev;
}, []);
Run Code Online (Sandbox Code Playgroud)
它作为普通 JS 工作得很好,但 TS 编译器不喜欢它。游乐场链接
如何解决这个问题?主要是我做错了什么?我是TS菜鸟。
错误:
No overload matches this call.
Overload 1 of 3, '(callbackfn: (previousValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentIndex: …Run Code Online (Sandbox Code Playgroud) 从sequelize 文档中获取这个简单的示例。
const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory');
class User extends Model {}
User.init(
{
// Model attributes are defined here
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
// allowNull defaults to true
},
},
{
// Other model options go here
sequelize, // We need to pass the connection instance
modelName: 'User', // We need to choose the model name
},
);
async function main() {
await User.sync(); …Run Code Online (Sandbox Code Playgroud) 我是一个 TS 菜鸟,但一直在使用 TS 编写一些小型服务。我正在开发一个基于node-redis的 CLI 工具。很棒的 Redis 客户端。
client以下代码片段中的类型是什么?
import { createClient } from "redis";
let client; // what is type of `client`?
client = createClient();
Run Code Online (Sandbox Code Playgroud)
根据使用情况推断类型如下。有办法清理这个吗?提前致谢。
import { createClient, RedisClientType, RedisScripts } from "redis";
let client: RedisClientType<{ json: { ARRAPPEND: typeof import("@node-redis/json/dist/commands/ARRAPPEND"); arrAppend: typeof import("@node-redis/json/dist/commands/ARRAPPEND"); ARRINDEX: typeof import("@node-redis/json/dist/commands/ARRINDEX"); arrIndex: typeof import("@node-redis/json/dist/commands/ARRINDEX"); ARRINSERT: typeof import("@node-redis/json/dist/commands/ARRINSERT"); arrInsert: typeof import("@node-redis/json/dist/commands/ARRINSERT"); ARRLEN: typeof import("@node-redis/json/dist/commands/ARRLEN"); arrLen: typeof import("@node-redis/json/dist/commands/ARRLEN"); ARRPOP: typeof import("@node-redis/json/dist/commands/ARRPOP"); arrPop: typeof import("@node-redis/json/dist/commands/ARRPOP"); ARRTRIM: typeof import("@node-redis/json/dist/commands/ARRTRIM"); arrTrim: typeof import("@node-redis/json/dist/commands/ARRTRIM"); …Run Code Online (Sandbox Code Playgroud) 假设脚本已导航到特定页面。如何在该脚本中执行js函数?
describe("TestSuite", () => {
test("Login", async() => {
await page.goto(APP);
await page.waitForSelector("[name=loginForm]");
await page.click("input[name=username]");
await page.type("input[name=username]", user.username);
await page.click("input[name=pwd]");
await page.type("input[name=pwd]", user.pwd);
await page.click("input[name=login]");
await page.waitForSelector(".PageBodyVertical");
// execute a js function x() here which is loaded with the page.
}, 60000);
Run Code Online (Sandbox Code Playgroud)