简体   繁体   中英

Typescript: Define type by string

I saw below evaluation in some external sources (that I want to use in my project):

const INIT: 'jsonforms/INIT' = 'jsonforms/INIT'
  1. What does this mean (defining type by a string ( INIT: 'jsonforms/INIT' ))? How a string ( 'jsonforms/INIT' ) can be a type?

    What is this called officially?

  2. My Webpack complains about this during build and needs additional loader :

     styleTagTransform.js:16 Uncaught Error: Module parse failed: Unexpected token (36:17) File was processed with these loaders: *./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js *./node_modules/source-map-loader/dist/cjs.js You may need an additional loader to handle the result of these loaders. | import { ErrorTranslator, Translator } from '../i18n'; | > export const INIT: 'jsonforms/INIT' = 'jsonforms/INIT'; | export const UPDATE_CORE: 'jsonforms/UPDATE_CORE' = `jsonforms/UPDATE_CORE`; | export const SET_AJV: 'jsonforms/SET_AJV' = 'jsonforms/SET_AJV';

Another complaination is here:

export type CoreActions =
  | InitAction
  | UpdateCoreAction
  | UpdateAction;

What does the above syntax mean?!

Uncaught Error: Module parse failed: Unexpected token (65:7)
File was processed with these loaders:
 * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
 * ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| export const REMOVE_DEFAULT_DATA = `jsonforms/REMOVE_DEFAULT_DATA`;
| 
> export type CoreActions =
|   | InitAction
|   | UpdateCoreAction

How a string ('jsonforms/INIT') can be a type?

In TypeScript, this type is a literal .

 export type CoreActions = | InitAction | UpdateCoreAction | UpdateAction;

What does the above syntax mean?!

This one is an union .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM