最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get suggestion in typescript with "type JustifyContent = 'center' | 'flex-s

programmeradmin0浏览0评论

Here is the code:

type JustifyContentProperty = 'center' | 'flex-start' | string

const justifyContent:JustifyContentProperty = 'cen..' // I expected 'center' suggests, but none.

Question:

  1. How can I get property suggestion with this type? expected showing 'center' 'flex-start', but because of "| string" definition it shows nothing.

  2. I met this problem because csstype library has a type -- CSS.JustifyContentProperty which can't trigger expected suggestions because of '| string', I wander if any other solution?

Here is the code:

type JustifyContentProperty = 'center' | 'flex-start' | string

const justifyContent:JustifyContentProperty = 'cen..' // I expected 'center' suggests, but none.

Question:

  1. How can I get property suggestion with this type? expected showing 'center' 'flex-start', but because of "| string" definition it shows nothing.

  2. I met this problem because csstype library has a type -- CSS.JustifyContentProperty which can't trigger expected suggestions because of '| string', I wander if any other solution?

Share Improve this question edited Jul 10, 2020 at 2:34 Yokiijay asked Jul 9, 2020 at 19:14 YokiijayYokiijay 8214 gold badges10 silver badges19 bronze badges 2
  • Why not just define type JustifyContentProperty = 'center' | 'flex-start'? – Baboo Commented Jul 9, 2020 at 19:18
  • Because the type is es from a library -- csstype – Yokiijay Commented Jul 10, 2020 at 2:00
Add a ment  | 

2 Answers 2

Reset to default 12

This requires a trick:

type JustifyContentProperty = 'center' | 'flex-start' | string & {}

With this change, any string is still assignable to JustifyContentProperty, but you will get IntelliSense for 'center' | 'flex-start'.

I have 2 solutions, IDE will give you type suggestions

// solution 1
type JustifyContentProperty = 'center' | 'flex-start' 

// solution2 use const enum
const enum position {
  'center',
  'flex-start'
}
const justifyContent = position.center

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论