Extension For "As" Proposal

I really love this idea by @yinon. He proposes that we could use an as keyword to merge the given props of an object into a new one.

const { x, y } as coordinates = point;

Sadly, it's not showing any signs of progress. So I'd like to add some new use cases for that.

Exports

It's not uncommon when we need to export some variables as a new object.

let x = 0;
let y = 1;

export { x, y } as coordinates;

We can do it this way too. But it pollutes the namespace.

export const coordinates = { x, y };

Imports

We can import and encapsulate only what we need like this...

import { createElement, FC, useState } as React from "react";

Function Arguments

function plot({ x, y } as coordinates) {
  // coordinates = { x, y }
}

Thank you :grinning:.

1 Like

Use of "as" patterns. Nice!
will there be TS like { ...obj } as const.
:wink: Anyway it looks good to me.
EDIT:
You might want to look at mdn modules for currently supported as patterns.
{ a as b, c as d } is currently supported! you might want to look into it!

1 Like

Thanks!

I'd like to have that too. But Object.freeze() may make it look unnecessary. This syntax can also clash with Typescript's type assertions :slightly_frowning_face:.