-
Notifications
You must be signed in to change notification settings - Fork 13k
Description
π Search Terms
readonly keys, mutable keys, utility type, extract readonly, readonly properties, key filtering, property keys, readonly extraction, conditional mapping
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Add built-in support for detecting readonly properties in TypeScript's conditional types system. This would enable developers to create custom utility types that can distinguish between readonly and mutable properties.
Currently, there's no reliable way to detect if a property is readonly using TypeScript's type system. This language enhancement would enable:
- Better type safety in state management libraries
- Safer form handling with readonly fields
- More precise API typing for update operations
This is not a request for new utility types, but rather a language-level improvement that would enable the community to build such utilities themselves.
π Motivating Example
With this language enhancement, developers could write:
interface User {
readonly id: string;
readonly createdAt: Date;
name: string;
email: string;
}
// Community could then create:
type ReadonlyKeys = ... // Using the new language feature
type MutableKeys = ... // Using the new language feature
// Enable safer patterns:
function updateUser(user: User, updates: OnlyMutable) {
return { ...user, ...updates };
}
π» Use Cases
1. Form Libraries
interface FormConfig {
fields: Record<MutableKeys, FieldConfig>;
// Only allow form fields for mutable properties
}
2. State Management
function createReducer(
initialState: T,
mutations: Record<string, (state: T, payload: any) => Pick<T, MutableKeys>>
) {
// Ensure reducers only update mutable fields
}
3. API Design
interface Entity {
readonly id: string;
readonly version: number;
name: string;
description: string;
}
// Auto-generate update DTOs that exclude readonly fields
type UpdateEntityDTO = Pick<Entity, MutableKeys>;
// Result: { name: string; description: string }
4. Database ORMs
class Repository {
update(id: string, data: Pick<T, MutableKeys>): Promise {
// Prevent updates to readonly fields like id, createdAt, etc.
}
}
5. Configuration Objects
interface Config {
readonly environment: 'dev' | 'prod';
readonly version: string;
debug: boolean;
apiUrl: string;
}
// Allow runtime updates only to mutable config
type RuntimeConfig = Pick<Config, MutableKeys>;