Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rule proposal: preferas const #312

Closed
Assignees
armano2
Labels
@mohsen1

Description

@mohsen1

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

typeStyles={position:'relative'|'absolute'}letstyles={position:'relative'as'relative'}lets: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 theas 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:

typeStyles={position:"relative"|"absolute";};letstyles={position:"relative"asconst};lets:Styles=styles;

More examples:

leta:'str'='str';// badleta=<'str'>'str';// badleta='str'as'str'//badleta='str'asconst// good
// badleto={foo:'string'as'string'}// goodleto={foo:'string'asconst}

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp