Skip to content

Const assertions #7970

@goodmind

Description

@goodmind

Proposal

https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#const-assertions

const foo = ([1, 2, 3]: const) // Type is [+1, +2, +3] (how to do read-only elements on tuple?)
const bar = ('foo': const) // Type is 'foo'
const baz = ({foo: 1}: const) // Type is '{| +foo: 1 |}'

Use case

More explicit version of #7607
Also would eliminate need for Object.freeze as tool for casting to singleton strings
Object.freeze is also a bit broken with regards to subtyping #7964

Not sure how sound it is, given that it is type cast essentially

Alternative

const foo = Object.freeze('foo') // Type is 'foo'
const baz = Object.freeze(1) // Type is 1
const bar = Object.freeze([1, 2, 3]) // Type is [+1, +2, +3] (how to do read-only elements on tuple?)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions