Skip to content

Rule proposal: prefer as const #312

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

Closed
mohsen1 opened this issue Feb 23, 2019 · 2 comments · Fixed by #1431
Closed

Rule proposal: prefer as const #312

mohsen1 opened this issue Feb 23, 2019 · 2 comments · Fixed by #1431
Assignees
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Feb 23, 2019

In a lot of our code we have to cast string literals to literals types to conform to types so assignments work:

  type Styles = {
    position: 'relative' | 'absolute'
  }
  
  let styles = {
    position: 'relative' as 'relative'
  }
  
  let s: Styles = styles 

This is more common when using React HOCs but above example demonstrate the problem well enough.

With TypeScript 3.4 we'll have the as const literal expression which should be preferred to string or number literal types in this case because changing the value does not require changing the type.

Our example code would be better written as:

type Styles = {
  position: "relative" | "absolute";
};

let styles = {
  position: "relative" as const
};

let s: Styles = styles;

More examples:

let a: 'str' = 'str'; // bad 
let a = <'str'>'str'; // bad 
let a = 'str' as 'str' //bad 

let a = 'str' as const // good
// bad
let o = {
    foo: 'string' as 'string'
}

// good
let o = {
    foo: 'string' as const
}

Rule name

Please suggest rule name. I'm not sure how this rule should be named

Options

I can't think of any configurability for this rule

@mohsen1 mohsen1 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 23, 2019
@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for team members to take a look labels Feb 25, 2019
@bradzacher
Copy link
Member

on question i'd have, is it worth hoisting the const up if we detect multiple within the same object?

i.e.

const styles = {
	display: 'flex' as 'flex',
    position: 'absolute' as 'absolute',
};

// does it fix to
const styles = {
	display: 'flex' as const,
    position: 'absolute' as const,
};
// pr
const styles = {
	display: 'flex',
    position: 'absolute',
} as const;

I have only briefly toyed with as const via the next tag, so I don't know if the latter example will do what I think it does..

@ckknight
Copy link

I don't think it'd be appropriate to fix to moving the as const to the object, as that has a dramatically different meaning (that all the properties are readonly)

@armano2 armano2 self-assigned this Jan 11, 2020
@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants