我在其他地方找不到这方面的信息。我想创建一个既具有定义的字符串又允许使用任何类型的字符串的类型,但会为定义的条目提供自动建议。
现在如果我添加 //| string
最后,它有点接管,并且不给出任何自动建议。
// if I add this it stops suggestions
type DefinedLetters = 'A' | 'B' | 'C' // | string
const Letter: DefinedLetters = '' // Should suggest A, B or C, but also allow D, E, F, etc.
最佳答案
我发现这个很酷的 LiteralUnion
类型正在为你研究这个问题,我认为它很有魅力!
type LiteralUnion<T extends U, U = string> = T | (U & { });
type DefinedLetters = LiteralUnion<'A' | 'B' | 'C'>
const Letter: DefinedLetters = "Cool!"
关于javascript - Typescript 声明已定义字符串或任何字符串的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58550698/