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

Literal narrowing lost without explicit const assertion #39763

Closed
Aprillion opened this issue Jul 27, 2020 · 3 comments
Closed

Literal narrowing lost without explicit const assertion #39763

Aprillion opened this issue Jul 27, 2020 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Aprillion
Copy link

Aprillion commented Jul 27, 2020

TypeScript Version: 3.9.2

Search Terms:
const assertion, literal narrowing

Code

const a1 = 'A1'
const a2 = 'A2'

const actionCreators = {
    a1: () => ({type: a1}),
    a2: (b: number) => ({type: a2, b}),
}
type Action = ReturnType<typeof actionCreators[keyof typeof actionCreators]>

export default (state = {}, action: Action) => {
    switch (action.type) {
        case a2: 
            return {...state, b: action.b}
        default:
            return state
    }
}

Expected behavior:
Code with literal narrowing should compile, similar to the following code with an explicit const assertion:

const a1 = 'A1' as const
const a2 = 'A2' as const
...

Actual behavior:
Code does not compile.

Property 'b' does not exist on type '{ type: string; } | { type: string; b: number; }'.
  Property 'b' does not exist on type '{ type: string; }'.(2339)

Related Issues:
Might be related to #9998.

Output
const a1 = 'A1'; // as const
const a2 = 'A2'; // as const
const actionCreators = {
    a1: () => ({ type: a1 }),
    a2: (b) => ({ type: a2, b }),
};
export default (state = {}, action) => {
    switch (action.type) {
        case a2:
            return Object.assign(Object.assign({}, state), { b: action.b });
        default:
            return state;
    }
};
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@ahejlsberg
Copy link
Member

This is working as intended. See #11126, and also #29510.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 27, 2020
@Aprillion
Copy link
Author

I see... Thanks for the explanation provided in the linked PRs - but my intuition was built from IDEs type hint tooltips, neither VS Code nor IntelliJ distinguish between the 2 different types (widening and non-widening literals both look the same):

vscode type tooltip

I wanted to ask where it's documented, but now that I know what to look for, I found it in mentioned in cost assertions section:

no literal types in that expression should be widened (e.g. no going from "hello" to string)

The feature was very hard to discover for me (and surprising when I did), but I understand how it makes sense I guess, so I will either get used to it or wait for a new readonly-by-default mode proposed in #32758.

@ahejlsberg
Copy link
Member

my intuition was built from IDEs type hint tooltips, neither VS Code nor IntelliJ distinguish between the 2 different types (widening and non-widening literals both look the same)

Yeah, we actually discussed displaying something to distinguish between the two, but we decided against the additional noise as in most cases it doesn't matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants