Skip to content

Want to undefine some built-in functions #13508

@gdh1995

Description

@gdh1995

I want a keyword undeclare on declared interface/class methods and var which works where it's referenced. It will benefit developers who wants stricter constraints.

Syntactic

It should be like these:

undeclare var Symbol; // never needs Symbol

// a chrome extension restricted by CSP will wants setTimeout only to accept valid functions
undeclare function setTimeout(handler: any, timeout?: any, ...args: any[]): number;
// accept extra args which is not declared in `lib.dom.d.ts`
declare function setTimeout(handler: (...args: any[]) => void, timeout: number, ...args: any[]): number;

And the declaration after undeclare should be strictly matched:

  • if a var/class/namespace/interface is undeclare-ed, then only remove one record with the given type
  • if a (member) function is undeclared, types of args and its returned value should be strictly matched.

Motivation

Many EcmaScript functions are generic for backward compatibility and usability, but developers may have their own programming standard, and only want limited usages. For example:

  • a CSP of script-src: 'self' limits setTimeout and refuses eval.
  • some APIs are deprecated and replaced by new ones.
  • a Chrome extension knows that it will has a complete chrome.tabs.Tab and tab.title is not string | undefined but string.

In my tests, I'm able to override definitions of member variables, so there has been a work-around for cases like tab.title (edit: it reported "All declarations of 'title' must have identical modifiers"), but I can not find any way to remove function definitions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions