gpt4 book ai didi

javascript - Angular 的 HashId

转载 作者:搜寻专家 更新时间:2023-10-30 21:27:56 24 4
gpt4 key购买 nike

我正在尝试使用 http://hashids.org在 Angular Last 版本项目中。

我找到了这个定义文件:

// Type definitions for Hashids.js 1.x
// Project: https://github.com/ivanakimov/hashids.node.js
// Definitions by: Paulo Cesar <https://github.com/pocesar>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/// <reference types="node" />

export default class Hashids {
private version: string;
private minAlphabetLength: number;
private sepDiv: number;
private guardDiv: number;
private errorAlphabetLength: string;
private errorAlphabetSpace: string;
private alphabet: string[];
private seps: string;
private minHashLength: number;
private salt: string;
constructor(salt: string, minHashLength?: number, alphabet?: string);
public decode(hash: string): number[];
public encode(arg: number): string;
public encode(arg: number[]): string;
public encode(...args: number[]): string;
public encodeHex(str: string): string;
public decodeHex(hash: string): string;
public hash(input: number, alphabet: string): string;
public unhash(input: string[], alphabet: string): number;
}

但是当我尝试使用这段代码在我的 Angular 项目中使用时:

import * as Hashids from 'hashids';
export abstract class BaseService {
protected getId(id: any) {
const x = new Hashids('somesecretec');
return x.encode(id);
}
}

我遇到了这个错误:

error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

在我的本地它可以正常工作,没有问题。但我试图编译一个生产设置,但它不起作用。

最佳答案

问题出在 Hashids 导入方式上,使用下面的选项将 hashids 作为默认值导入

import Hashids from 'hashids';

使用 * 或 {} 进行命名导入,Hashids 在导入为命名导入时抛出以下错误(名称默认值除外)

error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

使用默认名称(因为默认导出也是名称为默认的命名导出)

import {default as Hashids} from "hashids";

关于javascript - Angular 的 HashId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com