TypeScript和RegExp [英] TypeScript and RegExp

查看:495
本文介绍了TypeScript和RegExp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeScript说:

TypeScript says:


$ 1 属性不存在于类型' {的值{(pattern:string,flags?:string):RegExp; new(pattern:string,flags?:string):RegExp; } '

The property '$1' does not exist on value of type '{ (pattern: string, flags?: string): RegExp; new(pattern: string, flags?: string): RegExp; }'

通过查看 lib中的定义可以解释该类型。 d.ts 附带 TypeScript 0.8.2

interface RegExp {
    /** 
      * Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
      * @param string The String object or string literal on which to perform the search.
      */
    exec(string: string): RegExpExecArray;
    /** 
      * Returns a Boolean value that indicates whether or not a pattern exists in a searched string.
      * @param string String on which to perform the search.
      */
    test(string: string): bool;
    /** Returns a copy of the text of the regular expression pattern. Read-only. The rgExp argument is a Regular expression object. It can be a variable name or a literal. */
    source: string;
    /** Returns a Boolean value indicating the state of the global flag (g) used with a regular expression. Default is false. Read-only. */
    global: bool;
    /** Returns a Boolean value indicating the state of the ignoreCase flag (i) used with a regular expression. Default is false. Read-only. */
    ignoreCase: bool;
    /** Returns a Boolean value indicating the state of the multiline flag (m) used with a regular expression. Default is false. Read-only. */
    multiline: bool;

    lastIndex: number;
}
declare var RegExp: {
    new (pattern: string, flags?: string): RegExp;
    (pattern: string, flags?: string): RegExp;
}

我的问题是如何更改/更新此内容以允许我参考 RegExp。$ 1 RegExp。$ 2 等?最好我可以单独声明,因为我不打算直接编辑 lib.d.ts (很可能会在更新后更换)

My question is how can I change/update this to allow me to reference RegExp.$1, RegExp.$2, etc.? Preferably something I can declare separately since I do not intend to directly edit lib.d.ts (which will most likely get replaced as it is updated)

我试过这个无济于事:

declare var RegExp: {
    new (pattern: string, flags?: string): RegExp;
    (pattern: string, flags?: string): RegExp;
    $1: any;
    $2: any;
    $3: any;
    $4: any;
    $5: any;
    $6: any;
    $7: any;
    $8: any;
    $9: any;    
}

我猜他们应该用类型 string <来声明/ code>实际上。但无论如何它都不会删除错误。

I guess they should be declared with type string actually. But in any case it doesn't remove the errors.

除此之外我还很好奇这究竟是什么意思(为什么声明有没有?):

In addition to this I'm also curious about what this means exactly (why is it declared with and without a new?):

new (pattern: string, flags?: string): RegExp;
(pattern: string, flags?: string): RegExp;


推荐答案

您的声明var 已经关闭。接口是开放的,所以你可以写:

Your declare var is close. Interfaces are open, so you can just write:

interface RegExp {
    $1: string;
    $2: string;
    // etc
}

关于第二个,这是因为新的RegExp('[AZ]') RegExp('[AZ]')都可以创建一个RegExp(在类型术语,它既是一个可调用函数又是一个构造函数。)

Regarding the second one, it's because new RegExp('[A-Z]') and RegExp('[A-Z]') both work to create a RegExp (in type terms, it's both an invokable function and a constructor function).

这篇关于TypeScript和RegExp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆