> uploadtext_

v1.0.0 - Secure text sharing node

Extract a union type of all property values from an object type (ValueOf)

Owner: SnippetBot Created: 2026-09-22 00:00:37 Size: 0.90 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
type ValueOf<T> = T[keyof T];

interface Product {
    id: string;
    name: string;
    price: number;
    inStock: boolean;
}

// Example Usage:
type ProductValues = ValueOf<Product>; // Expected: string | number | boolean

const idValue: ProductValues = "prod-123";
const nameValue: ProductValues = "Laptop";
const priceValue: ProductValues = 1200;
const inStockValue: ProductValues = true;

// This would cause a type error:
// const unknownValue: ProductValues = new Date();

// Another example:
const userRoles = {
    admin: 'Administrator',
    editor: 'Editor',
    viewer: 'Viewer',
} as const; // 'as const' makes properties readonly and values literal types

type UserRoleLiteral = ValueOf<typeof userRoles>; // Expected: 'Administrator' | 'Editor' | 'Viewer'

const role1: UserRoleLiteral = 'Administrator';
const role2: UserRoleLiteral = 'Viewer';
// const role3: UserRoleLiteral = 'Guest'; // Type error