添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type. #4605 A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type. #4605 iliyaZelenko opened this issue Mar 26, 2019 · 2 comments "defaultSeverity" : " warning " , "extends" : [ " tslint:latest " , " tslint-config-standard " ], "rules" : { "no-console" : false , "member-access" : [ true , " no-public " ], "object-literal-key-quotes" : false , "no-var-requires" : false , "interface-name" : [ true , " never-prefix " ], "prefer-const" : true , "ordered-imports" : [ false ], "object-literal-sort-keys" : [ false ], "arrow-parens" : false , "no-implicit-dependencies" : false , "no-submodule-imports" : [ true , " ~ " , " @ " ], "quotemark" : [ true , " single " ], "semicolon" : [ true , " never " ], "trailing-comma" : [ true , { "multiline" : " never " , "singleline" : " never " "space-before-function-paren" : true , "restrict-plus-operands" : false

Actual behavior

51:12 A computed property name in a class property declaration must refer to an
expression whose type is a literal type or a 'unique symbol' type.
    49 | })
    50 | export default class Main extends Vue {
  > 51 |   readonly [PROPS.VALUE]: string

Expected behavior

There was no error.

Hi @iliyaZelenko! This repository is for TSLint, a program that uses TypeScript APIs to provide additional analysis on TypeScript code. The error you're receiving is from TypeScript itself. Those issues should be filed on https://github.com/Microsoft/TypeScript.

Note: the reason why TypeScript is complaining is because PROPS.VALUE is itself not a literal type or 'unique symbol' type. It would be valid for someone to do this:

const PROPS = {
  VALUE: 'value'
PROPS.VALUE = 'aha!';
export default class Main extends Vue {
  readonly [PROPS.VALUE]: string

Once TypeScript 3.4 is released (very soon!), you'll want to use a const assertion. Until then you can typecast VALUE to be as 'value':

const PROPS = {
  VALUE: 'value' as 'value'

Without an as const or as 'value', the type of PROPS.VALUE is general string.

@JoshuaKGoldberg, thank you so much for the answer. Sorry for creating the question in the wrong place.

Thanks to you, now I understand what the "literal type" is.

Thanks for telling me what they will add const assertion in version TypeScript 3.4. Since my project is new, I installed version ^3.4-rc and the error disappeared (with as const).

@JoshuaKGoldberg, really thank you!

Type error causing build failure in the destination package: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type. adobe/react-spectrum-charts#272