English 中文(简体)
在标本中使用浏览器特定属性的正确方式是什么?
原标题:What s the proper way of using a browser specific property in typescript?
The bounty expires in 6 days. Answers to this question are eligible for a +50 reputation bounty. ADJenks is looking for an answer from a reputable source.

具体地说,我想在 FireFox 错误对象上使用“ 文件名称” 和其他额外属性。

But something like this gives me red underlining, which I could simply ignore, but it bothers me. I tried

function normalizeError(e:Error|ErrorEvent|PromiseRejectionEvent|any) {
    if(e instanceof Error){
        return {
            line: e?.fileName //underlined
        }
    }else return null
}

下划线是因为文件Name 是一个仅在浏览器子集中找到的属性, 它不是斯坦诺化的, 但它是我在参数类型中指定的错误对象上的属性 。

正在修改与声明界面的界面 合并工程排序...

interface Error {
    fileName?: string
}
function normalizeError(e:Error|ErrorEvent|PromiseRejectionEvent|any) {
    if(e instanceof Error){
        return {
            line: e?.fileName // Not-underlined
        }
    }else return null
}

但是,当我输出这个功能时,它又强调了它?

interface Error {
    fileName?: string
}
// Exported now:
export function normalizeError(e:Error|ErrorEvent|PromiseRejectionEvent|any) {
    if(e instanceof Error){
        return {
            line: e?.fileName // underlined again
        }
    }else return null
}

An image to show the underline: typescript underlining missing property

那么当我输出函数时,为什么不再申报合并工作呢? 我能做些什么去掉下划线呢?

增编1:

I noticed that if I reassign e to itself, there is no longer an error... What? See: typescript code assigning error to itself

显然指派e 将它从错误类型更改为“ 任何” 类型, 摆脱该财产并不存在错误。 我本可以假设,在基本上将它自己分配时,该类型不会改变,但我猜想我错了。

增编2:

我想我只是要使用 // @ts-ignore ,直到有人澄清用什么适当方式来澄清标注的正确方式,即属性 might actually 是可用的。

最佳回答

显然,我需要在全球宣布它们如此:

// global.d.ts

export {} // Exporting something is required

declare global {

    interface Error {
        /** Mozilla specific */
        lineNumber?:number,
        /** Mozilla specific */
        columnNumber?: number,
        /** Mozilla specific */
        fileName?: string,
    }
}

This method can be found here: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html

这似乎是我最合适的方法

有没有这种类型的官方图书馆?

@Rodrigo Rodrigues 开导我到另一个选项, 宣布一个扩展错误类的抽象类。 我不知道哪个选项更好。 最好不要在全球修改基本类型声明, 这样这个方法可能更好。 我测试了在同一个文件中添加这个选项, 它似乎有效 :

declare abstract class Error extends globalThis.Error {
    /** Mozilla specific */
    public lineNumber: number
    /** Mozilla specific */
    public columnNumber: number
    /** Mozilla specific */
    public fileName: string
}
问题回答

正如我在评论中所提到的,“href=”“https://stackoverflow.com/a/350200531/2938526”中显示的一种有趣的方法,是用来在这样的情况下使类型检查器快乐的:一个抽象的分类,扩展对象的类别。

declare abstract class Error extends globalThis.Error {
    public fileName: string
    // (...)
}

我认为,这比在全球范围覆盖你这种类型的宣言更好,原因有二:

  • It doesn t mess with the global namespace; you can restrict it tho the scope of your current module
  • Your new class derives from the original class, so type-checking works wonders and you can return or pass this object around and it will also work due to polymorphism.




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签