@ncobase/types
v0.1.3
Published
Common TypeScript types
Readme
@ncobase/types
Common TypeScript type definitions
Installation
pnpm add @ncobase/typesUsage
Environment Types
Environment related type definitions for configuration.
import type { HostVariables, Environment, EnvironmentConfig } from '@ncobase/types';
const config: EnvironmentConfig = {
development: {
HOST: 'localhost',
PORT: '3000',
PATH: '/api'
}
};Utility Types
General purpose utility types.
import type {
AsyncOrSync,
AsyncOrSyncReturn,
Overwrite,
PlainObject,
DeepPartial
} from '@ncobase/types';
// Async or sync function return type
type UserFetch = () => Promise<{ id: number }>;
type User = AsyncOrSyncReturn<UserFetch>; // { id: number }
// Deep partial type
interface Config {
server: {
host: string;
port: number;
};
database: {
url: string;
name: string;
};
}
type PartialConfig = DeepPartial<Config>;
const config: PartialConfig = {
server: {
host: 'localhost'
// port is optional
}
// database is optional
};React Types
React specific type definitions.
import type { ComponentProps, CSSProperties, EventHandler } from '@ncobase/types';
// Component props type
interface ButtonProps {
onClick: () => void;
children: React.ReactNode;
}
const styles: CSSProperties = {
color: 'blue',
fontSize: 16
};
// Event handler type
const handleClick: EventHandler<React.MouseEvent> = event => {
console.log('Clicked:', event.target);
};Available Types
Environment Types
HostVariables- Host environment variables configurationEnvironment- Environment type ('production' | 'development' | 'test')EnvironmentConfig- Environment configuration mapping
Utility Types
AsyncOrSync<T>- Type that can be T or PromiseAsyncOrSyncReturn<T>- Return type of async or sync functionOverwrite<T, U>- Merge two types, overwriting first type's properties with secondPlainObject- Object literal typeAnyObject- Any object typeObjectArray- Array of objectsDeepPartial<T>- Make all properties in T and its nested objects optionalDeepRequired<T>- Make all properties in T and its nested objects requiredNonNullable<T>- Remove null and undefined from TAwaited<T>- Unwrap Promise type
React Types
ComponentProps<T>- Extract props type from React componentCSSProperties- React CSS properties typeEventHandler<E>- React event handler type
