Is that possible to use typescript string type like this one? #1009
Answered
by
franky47
GrahamQuan
asked this question in
Q&A
|
cool package declare function useQueryState(key: string, options: Options & {
defaultValue: string;
}): UseQueryStateReturn<string, typeof options.defaultValue>;current is fixed string type ColorType = 'red' | 'green' | 'blue' | (string & {});
// 1
const [color, setColor] = useQueryState<ColorType>('color', {
defaultValue: 'red'
})
// or 2
const [color, setColor] = useQueryState('color', {
defaultValue: 'red' as ColorType
})Is that possible to use this typescript string type for auto complete ? |
Answered by
franky47
Jun 6, 2025
Replies: 1 comment
|
The issue here is the Runtime-wise, either is correct as it will accept any string, as defined by the type, but for some reason: type ColorType = 'red' | 'green' | 'blue' | (string & {});
type A = NonNullable<ColorType> // string
type B = Exclude<ColorType, null | undefined> // 'red' | 'green' | 'blue' | (string & {});This would also cause the same issue with the Prettify helper we use to resolve types in useQueryStates and other features. |
0 replies
Answer selected by
GrahamQuan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The issue here is the
NonNullabletype helper we use internally, which strips the explicit enumeration of values for autocompletion.Runtime-wise, either is correct as it will accept any string, as defined by the type, but for some reason:
This would also cause the same issue with the Prettify helper we use to resolve types in useQueryStates and other features.