Skip to content

Allow interface extends object syntax #60730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
6 tasks done
HansBrende opened this issue Dec 10, 2024 · 2 comments
Open
6 tasks done

Allow interface extends object syntax #60730

HansBrende opened this issue Dec 10, 2024 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@HansBrende
Copy link
Contributor

HansBrende commented Dec 10, 2024

πŸ” Search Terms

interface extends object

βœ… Viability Checklist

⭐ Suggestion

I propose to allow the following syntax:

interface MyInterface extends object {}

This would have identical semantics to the following (currently legal) syntax:

type _object = object
interface MyInterface extends _object {}

Details:

  1. No primitive may be assigned to such an interface or an intersection containing such an interface
  2. This requirement is inherited by any extending interfaces

I'm requesting this feature mainly because it was the only pain-point mentioned in this related issue. To quote:

No one is going to turn on this behavior if it means every time they write

interface Person {
  name: string
}

they actually need to write

interface _PersonFields {
  name: string;
}
type Person = object & _PersonFields;

Regardless of the outcome of that other issue however, it would be nice to be able to extends object without going through that kind of hassle.

πŸ“ƒ Motivating Example

A small usability improvement to allow interfaces to more easily declare that primitives cannot be assigned to them.

πŸ’» Use Cases

  1. What do you want to use this for? To avoid non-idiomatic verbosity
  2. What shortcomings exist with current approaches? Non-idiomatic verbosity
  3. What workarounds are you using in the meantime? Non-idiomatic verbosity
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 11, 2024
@RyanCavanaugh
Copy link
Member

This would have identical semantics to the following (currently legal) syntax: ...
A small usability improvement to allow interfaces to more easily declare that primitives cannot be assigned to them.

To be clear, this doesn't work today, which is why it's disallowed

type _obj = object;
interface LengthyObj extends _obj {
  length: number;
}
function fooObj(s: LengthyObj) { }
// No error...
fooObj("hello");

@HansBrende
Copy link
Contributor Author

@RyanCavanaugh yep, figured that out shortly after I wrote it (although interestingly it does work for the specific example I posted, which is the case of the empty interface).

type _obj = object
interface Empty extends _obj {}

const foo: Empty = 'hello';  // Error!

The fact that it stops working as soon as you add at least one property to the interface feels like a bug to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants