Issue
I'm writing a library, I have an object which I want to let the users of this library to get autocompletion on one of its properties, however I want them to be able to add other string too, my type is :
type Object2 = {
prop: "*" | "line" | "media" | string;
}
On my IDE (Intellij) and also compile output I get :
Object2: {
prop: string;
};
What I need to get however is the same as I defined "*" | "line" | "media" | string so what do I do I'm really stuck here
Solution
All string literal types get absorbed into string, because string is the base type of those literal types.
There is a way to prevent this, but intersecting string with {}
type Object2 = {
prop: "*" | "line" | "media" | (string & {});
}
Any string will still be assignable to the prop, but you will get suggestions in VS Code.
This is discussed here. The &{} solution is also suggested there.
Answered By - Titian Cernicova-Dragomir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.