I have this example
class Test {
firstName: string;
lastName:string;
}
Now I would like typescript to validate this
let text = "firstName:aName lastName:bName"
I know of keyof
but I want to take it forther and when I write fi
in a string it will be able to present firstName
and the same with lastName
as it gets seperated by space or comma.
Is this possible in typescript?
I am building a library a react-native
library that would make styling easier.
in react-native
you write style as
style={{color:"black", fontSize:12 etc}}
I am building a Library that will make it possible to shorter the style and make it easier to write so it would be as
css="co:black fos:12 bac:white etc.."
The issue is I would like typescript to be able to validate this and also be able to include some random user class that is inlcuded in user theme
css="co:black fos:12 bac:white container etc.."
container is a user define style that exists in the user theme so typescript needs not validate this. only validate names that are predefined as col
or color
I could build a cli that generates the type dynamicly but I am not sure how, the ts part how not the cli
2
Answers
You can split the string and validate its parts, if a substring is of format
key:value
, validate the key against your validation object and the value against allowed interpolatable values:Playground
If I understand you correctly, your question aims at having TS autocompleting for parts of a string.
I do not think this is possible.