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

Add unsafe sub- type definitions #23600

Closed
fgarcia opened this issue Apr 21, 2018 · 2 comments
Closed

Add unsafe sub- type definitions #23600

fgarcia opened this issue Apr 21, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@fgarcia
Copy link

fgarcia commented Apr 21, 2018

TypeScript Version: 2.8.3

Search Terms:

  • explicit cast
  • require cast
  • unsafe type

Code

function queryOutsideWorld(queryName) : unsafe {
   // ... some fetch() or RPC call
  // ... some JSON.parse()
  // ... extract body with random interface
  return someRandomData
}

// fail
let data = queryOutsideWorld('user-profile')

// ok
let data: UserProfile = queryOutsideWorld('user-profile')

Related Issues:

#10715
#22716
#9999

I've read similar issues and the closest one is the unknown type #10715. However I believe there is room for a less complex solution.

Currently if I fetch something from the outside world I must do some kind of runtime validation and cast to a known type / interface. If I start looking for common functionality I will be forced to create functions and even libraries that must return any but I would like a way return a value and force the programmer to do an explicit cast.

In my example I've used a new type keyword as a basic example, but I would rather like to have something where I can define unsafe subtypes such has:

// global.d.ts
type Unsafe extends any as unsafe
type RiskyAny extends any;

if (process.env.NODE_ENV === 'production') {
  type RiskyAny extends any as unsafe;
}

This would enable a second case. I want to prototype code/ideas in a non very strict environment, and only increase the typing-strictness later, if/when the library "matures". There is code that is OK to have type any in development, but in production (or at a more mature version) it will have to go away before other any types

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2018

looks like a duplicate of #10715

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 21, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants