Event JSON
{
"id": "ed7a194dc77b8d7d35e067a37f413dbbc25557015dc4a201e9fcf0aebc791726",
"pubkey": "bb1742d1b321126a78c9a65acb9b810a49693c1b76b6446ae059fc3ff0cc2bf2",
"created_at": 1718909548,
"kind": 1,
"tags": [
[
"p",
"692d9d8fb6c5db3d114c21513be89063e8ee0d14f89d35103c8f1b7d40a116a7"
],
[
"e",
"9ef941bdeb7154ba9c9e191c9be759f2c20d9e833738f2e99d38704d3e4f3b9a",
"",
"root"
],
[
"p",
"ceee9d7e6705946321b940eb4ec4db738c18362d2ac26d89c9eaaf9269c7fe0b"
],
[
"e",
"6cbcef835382351553aaa93f92212e443b91daa9b6b7007c021ba3beb212952e",
"",
"reply"
],
[
"proxy",
"https://techhub.social/@develwithoutacause/112650456197963453",
"web"
],
[
"proxy",
"https://techhub.social/users/develwithoutacause/statuses/112650456197963453",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://techhub.social/users/develwithoutacause/statuses/112650456197963453",
"pink.momostr"
]
],
"content": "TypeScript does not enforce the types of \"return only generics\" so it accepts whatever you give it. Like you mention, that's effectively a cast whether the developer knows it or not.\n\nA recommended style I've seen is to avoid return only generics and just return `unknown` in such situations, forcing the developer to explicitly cast the result unambiguously.\n\nThe tricky part here is `| undefined` which you'd still want to enforce and would be subsumed by the `unknown`. I think you could do something like `{} | undefined` (not sure `{}` is the right type). But as soon as you require a cast you're losing the `undefined` constraint too.\n\n```\nconst foo = getData('test', {noThrow: true}) as string;\n// ^ Didn't handle `undefined`.\n```\n\nI think the best approach would be to return a `Result` type or discriminated union of some kind so it forces you to \"unbox\" the inner `unknown` by checking existence. I don't see a better way here.",
"sig": "f013ef92bfdf53e524351e8bf97649bf8009378e5ca8b291eb67f47fb05c6eb20ef8a22b6c80c5a8658f541522ff7cdfbca66dcc7acdc9401e75db03695c7673"
}