為什么env不能enum type包含串列的所有專案,以及如何?
const { hostname, pathname } = window.location
const rootDir = pathname.split('/')[1]
const list = ['newstage', 'stage', 'qa3', 'qa2', 't1', 'sit', 'dev']
const env = list.find(el => rootDir === el)
現在env是const env: string,我想要的是
const env: "stage" | "qa3" | "qa2" | "t1" | "sit" | "dev"
uj5u.com熱心網友回復:
嘗試使用const 斷言:
不應該擴大該運算式中的文字型別(例如,不要從“hello”變為字串)
const rootDir = 'qa3';
const list = ['newstage', 'stage', 'qa3', 'qa2', 't1', 'sit', 'dev'] as const;
const item = list.find(el => rootDir === el)
打字稿游樂場
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407269.html
標籤:
