feat: define core types for virtual file system#12
Conversation
- Add FileSystemNode, File, Directory interfaces - Add FileSystem interface with CRUD operations - Add isFile and isDirectory type guards - Add tests for type guards Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| root: Directory; | ||
| exists(path: string): boolean; | ||
| get(path: string): FileSystemItem | null; | ||
| mkdir(path: string): Directory; |
There was a problem hiding this comment.
Consider adding return type documentation or handling for existing paths:
mkdir: Should it throw if the directory already exists, or return the existing one?touch: Should it update content if the file exists, or throw?
| import { isFile, isDirectory, File, Directory } from "./types"; | ||
|
|
||
| describe("types", () => { | ||
| describe("isFile", () => { |
There was a problem hiding this comment.
Consider adding tests for edge cases like null/undefined input to ensure type guards are robust.
|
|
||
| export type FileSystemItem = File | Directory; | ||
|
|
||
| export interface FileSystem { |
There was a problem hiding this comment.
The FileSystem interface doesn't specify error handling behavior. Consider documenting:
- What happens when paths don't exist (return null vs throw)
- Path validation (empty strings, invalid characters)
- Potential path traversal security concerns for the implementation
|
|
||
| export interface Directory extends FileSystemNode { | ||
| isDirectory: true; | ||
| children: FileSystemNode[]; |
There was a problem hiding this comment.
The children array uses FileSystemNode[] but could be more precise with FileSystemItem[] to ensure type safety when iterating (currently requires type narrowing). Consider using FileSystemItem[] instead.
SummaryThis PR introduces core TypeScript interfaces for a virtual file system including Critical IssuesNone - the implementation is solid for a foundational types PR. Recommendations
Positive Notes
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code ReviewSummaryThe PR introduces core TypeScript interfaces for a virtual file system with proper type guards and tests. The implementation is clean and well-structured. Critical IssuesNone - the core types are well designed. Recommendations
Positive Notes
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| describe("types", () => { | ||
| describe("isFile", () => { | ||
| it("returns true for File", () => { | ||
| const file: File = { |
There was a problem hiding this comment.
Test objects are missing required properties from FileSystemNode (name, path, createdAt, modifiedAt). While this works because the tests directory isn't included in TypeScript compilation, it would be better to include these properties for correctness and to serve as documentation.
|
|
||
| export type FileSystemItem = File | Directory; | ||
|
|
||
| export interface FileSystem { |
There was a problem hiding this comment.
Consider adding JSDoc comments to document: whether paths are absolute or relative, what the return values mean (e.g., what does rm returning boolean signify?), and error handling behavior for these methods.
Review SummarySummary: This PR introduces core TypeScript types for a virtual file system, including Critical Issues: None - this is a solid type definition PR. Recommendations:
Positive Notes:
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| name: string; | ||
| path: string; | ||
| isDirectory: boolean; | ||
| createdAt: Date; |
There was a problem hiding this comment.
Consider using string (ISO date format) instead of Date for createdAt and modifiedAt. Date objects are not directly serializable to JSON, which could cause issues if the virtual file system needs to be persisted or transmitted.
| path: string; | ||
| isDirectory: boolean; | ||
| createdAt: Date; | ||
| modifiedAt: Date; |
There was a problem hiding this comment.
Consider using string (ISO date format) instead of Date for createdAt and modifiedAt. This ensures consistency and improves serializability.
| export type File = FileSystemNode & { | ||
| isDirectory: false; | ||
| content: string; | ||
| size: number; |
There was a problem hiding this comment.
Consider documenting the unit for size (e.g., bytes) or creating a type alias to make it explicit.
|
|
||
| export type FileSystemItem = File | Directory; | ||
|
|
||
| export interface FileSystem { |
There was a problem hiding this comment.
The FileSystem interface defines method signatures but not their implementation. Consider documenting that this is intentionally an interface for the API contract, or if there's a default implementation class planned.
SummaryThis PR introduces core TypeScript interfaces for a virtual file system, including Critical Issues
Recommendations
Positive Notes
|
- Use string (ISO 8601) instead of Date for serialization - Add SizeInBytes type alias for clarity - Add JSDoc for FileSystem interface Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
|
||
| export type Directory = FileSystemNode & { | ||
| isDirectory: true; | ||
| children: FileSystemNode[]; |
There was a problem hiding this comment.
The children array should be typed as FileSystemItem[] (which is File | Directory) rather than FileSystemNode[]. This ensures type safety when using the type guards (isFile, isDirectory) on children, since those guards expect FileSystemItem but will receive FileSystemNode.
| write(path: string, content: string): File; | ||
| } | ||
|
|
||
| export function isFile(item: FileSystemItem): item is File { |
There was a problem hiding this comment.
The type guards accept FileSystemItem but will be used on Directory.children which contains FileSystemNode items. Either widen the parameter type to FileSystemNode or ensure children are typed as FileSystemItem[] (see line 19).
| get(path: string): FileSystemItem | null; | ||
| mkdir(path: string): Directory; | ||
| touch(path: string, content?: string): File; | ||
| rm(path: string): boolean; |
There was a problem hiding this comment.
Consider if rm should return boolean or void. Returning boolean is unconventional for file deletion - typically it's either void (with exceptions on failure) or the deleted item. This could be confusing to API consumers.
Review SummaryPositive Notes
Critical Issues
Recommendations
Documentation
Overall: Good foundation for the virtual file system types. The main concern is the type consistency between |
- Use Date objects for createdAt/modifiedAt - Fix children type to FileSystemItem[] for type safety - Update type guards to accept FileSystemNode - Change rm() return type to void - Add JSDoc comments to all exports Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| export type FileSystemNode = { | ||
| name: string; | ||
| path: string; | ||
| isDirectory: boolean; |
There was a problem hiding this comment.
Consider removing isDirectory from FileSystemNode since it's redundant with the discriminated union pattern where File has isDirectory: false and Directory has isDirectory: true. The type guard already narrows correctly based on the subtype.
| @@ -0,0 +1,63 @@ | |||
| export type SizeInBytes = number; | |||
There was a problem hiding this comment.
Consider adding JSDoc for this type to document what SizeInBytes represents and any constraints (e.g., must be non-negative).
| @@ -0,0 +1,58 @@ | |||
| import { describe, it, expect } from "vitest"; | |||
| import { isFile, isDirectory, File, Directory } from "../src/types"; | |||
There was a problem hiding this comment.
The test file is importing from ../src/types but the package.json tsconfig.json only includes src folder. The tests should either be in src/ or you need to update the tsconfig to include tests. This is not a bug since vitest handles this, but type-checking won't apply to tests.
| /** | ||
| * Union type for file system items (files or directories). | ||
| */ | ||
| export type FileSystemItem = File | Directory; |
There was a problem hiding this comment.
Consider also exporting FileSystemItem since it's a useful union type that consumers may need for type annotations.
| import { describe, it, expect } from "vitest"; | ||
| import { isFile, isDirectory, File, Directory } from "../src/types"; | ||
|
|
||
| describe("types", () => { |
There was a problem hiding this comment.
Consider adding tests for FileSystemItem union type to verify type narrowing works correctly with both File and Directory variants.
Summary
FileSystemNode,File,Directory, andFileSystemisFile()andisDirectory()for runtime type checkingTest plan
pnpm test- all tests passpnpm typecheck- no type errors🤖 Generated with Claude Code