Skip to content
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

Proposal: Safe type assertion in expression #23104

Closed
kgtkr opened this issue Apr 3, 2018 · 6 comments
Closed

Proposal: Safe type assertion in expression #23104

kgtkr opened this issue Apr 3, 2018 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@kgtkr
Copy link

kgtkr commented Apr 3, 2018

Background

Currently, it is impossible to add type ascription only to variable declaration.
Also, casting is not safe.
I want a way to safely add type ascription in the middle of the expression.

Proposal

expression :: Type

Example

//equal: const x:[number,string]=[1,"str"];
const x=[1,"str"]::[number,string];
declare const x: {};
const a: { p: number } = x;//err
const a = x as { p: number };//ok
const a = x :: { p: number };//err
@MartinJohns
Copy link
Contributor

Could you expand a bit more on why you would want this? It just seems to be more syntax that needs to be maintained for a functionality that already exists and works.

@kgtkr
Copy link
Author

kgtkr commented Apr 3, 2018

example:

const obj={
  x:"abc"::"abc"|"xyz", 
  //more props
};
//verbose
const obj:{x:"abc"|"xyz",...}={
  x:"abc", 
  //more props
};
//unsafe
const obj={
  x:"abc" as "abc"|"xyz", 
  //more props
};

I gave a simple example, but I think that it will be more useful in various situations
It can be used for upcast etc.

@mhegazy mhegazy changed the title proposal:type ascription in expression Proposal: Safe type assertion in expression Apr 3, 2018
@mhegazy mhegazy added the Suggestion An idea for TypeScript label Apr 3, 2018
@krryan
Copy link

krryan commented Apr 3, 2018

Not wild about the :: syntax, but strongly support the idea.

As a work around, our project uses function to<T>(value: T): T { return value; } so that you can write something like

const obj = {
    x: to<'abc' | 'xyz'>('abc'),
    // more props
};

Kind of annoying to have to use this function, though, plus it incurs runtime overhead for no good reason.

@kgtkr
Copy link
Author

kgtkr commented Apr 4, 2018

I noticed that :: and esnext:bind-operator became ambiguous
I have to find another syntax

https://github.com/tc39/proposal-bind-operator

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Suggestion An idea for TypeScript labels Jul 30, 2018
@RyanCavanaugh
Copy link
Member

Same general problem as #7481

@chinoto
Copy link

chinoto commented Aug 25, 2022

7481 was closed due to length. Continued at #47920. I figure this might help anyone who lands here get up to speed quicker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants