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

Present somehow to user that literal type is a widening literal type #11181

Closed
normalser opened this issue Sep 27, 2016 · 2 comments
Closed

Present somehow to user that literal type is a widening literal type #11181

normalser opened this issue Sep 27, 2016 · 2 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@normalser
Copy link

normalser commented Sep 27, 2016

I'm just adding the task here for your consideration (because it was buried in a comment in some other bug).

With #11126

we have now (literal types and widening literal types) and new rules to remember to understand the resulting inferred type for mutable location:

Type Inferred type for mutable location
'foo' 'foo'
widening 'foo' string
'foo' | 'bar' 'foo' | 'bar'
'foo' | widening 'bar' string
'foo' | widening 'foo' 'foo'

etc..

Now using this code:

// File: SomeonesLibrary.ts
declare var val:boolean
export const a : 'a' | 'b' = (val) ? 'a' : 'b'
export const b = (val) ? 'a' : 'b'
// File: OurCode.ts
import * as test from './SomeonesLibrary'

// VSCode will show:
// test.a -> type: 'a' | 'b'
// test.b -> type: 'a' | 'b'

let a1 = test.a // Type: 'a' | 'b'
let a2 = test.b // Type: string  --->> why ??????? 
       // not easy to answer without going to source code of someones files 
       // to understand TS behavior


// Assume user did not check what TS infered for `a1` and `a2` because 
// natural thinking is that it is of type `a | b`
a1 = 'c' // Throws error nicely
a2 = 'c' // No error shown 

so what I'm asking is - make it more clear for user to figure out why TS behaves in a way it behaves (because right now presenting Type: a | b for both cases is misleading and can cause some bugs (or rather prevent bugs from detecting)

Maybe VSCode could show:

// test.a -> type: 'a' | 'b'
// test.b -> type: widening 'a' | widening 'b'     # or different color 

Thanks a lot for consideration

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Sep 27, 2016
@RyanCavanaugh
Copy link
Member

I'm not sure this reduces net confusion, but we can consider it

@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Nov 15, 2016
@RyanCavanaugh
Copy link
Member

It's not at all obvious what value this would provide -- anyone capable of understanding what that would mean probably wouldn't be confused in the first place, whereas anyone who doesn't know what widening "foo" means is going to be really lost

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

2 participants