Fix nip19 npub decoding with proper positive type guard

Use positive type guard (decoded.type === 'npub' && typeof decoded.data === 'string')
to properly narrow the union type. TypeScript requires both conditions to infer the
correct type for decoded.data when dealing with discriminated unions.
This commit is contained in:
shakespeare.diy committed 2025-10-15 01:33:41 -05:00
1 parent ecec383f2f
commit c037bd1376
1 file changed
+4 -2
+4 -2
View File
@@ -218,10 +218,12 @@ export function PullRequestDialog({
if (pubkeyOrNpub.startsWith('npub1')) {
try {
const decoded = nip19.decode(pubkeyOrNpub);
if (decoded.type !== 'npub') {
// Type guard: ensure we have an npub
if (decoded.type === 'npub' && typeof decoded.data === 'string') {
owner = decoded.data; // hex pubkey
} else {
throw new Error('Expected npub but got ' + decoded.type);
}
owner = decoded.data; // hex pubkey
} catch (err) {
console.error('Failed to decode npub:', err);
setError('Invalid npub in Nostr URL');