typesafe paths
#1704
Replies: 1 comment 1 reply
-
I have another approach, which uses https://github.com/GoogleFeud/ts-macros, I made some macros to compile object access style path back to string, so we don't have any overheads in runtime. function $withDelimiter<Shape extends PathAccessibleObject, Delimiter extends string>(): RetrievablePath<Shape, Delimiter> { return void 0 }
function $toKey<T extends RetrievablePath<any, any, any>>(input?: T): T['__path__'] {
return $$ts!($$typeToString!<T['__path__']>())
}
function $typeOnly<T>(input: T): T {
return void 0
}
interface i18n extends PathAccessibleObject {
get: 1
deep: {
access: {
is: 'here'
}
}
}
const _root = $withDelimiter!<i18n, '.'>()
const _deep = $typeOnly!(_root.deep)
const _access = $typeOnly!(_deep.access)
const t = $toKey!(_access.is) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am thinking of adding type safety to my project.
I made a path resolver to allow compile time check and add type hints.
to use it you can do
But we still have to covert it back to string. Does anyone have any better idea?
Beta Was this translation helpful? Give feedback.
All reactions