UNPKG

@evyweb/ioctopus

Version:

A simple IoC container for JavaScript and TypeScript for classes and functions.

35 lines (34 loc) 1.84 kB
import { createContainer } from './src'; console.log('=== Testing Typed Container ==='); // Test typed container const typedContainer = createContainer(); // Test bind autocomplete and type safety typedContainer.bind('USER_SERVICE').toValue({ getName: () => 'John' }); typedContainer.bind('LOGGER').toValue({ log: (msg) => console.log(msg) }); typedContainer.bind('CONFIG').toValue({ apiUrl: 'https://api.test.com', timeout: 5000 }); // Test get autocomplete - should only suggest 'USER_SERVICE', 'LOGGER', 'CONFIG' const userService = typedContainer.get('USER_SERVICE'); // Should autocomplete registry keys const logger = typedContainer.get('LOGGER'); const config = typedContainer.get('CONFIG'); // Verify full type inference const userName = userService.getName(); // Should have getName() intellisense logger.log('Hello World'); // Should have log() intellisense const url = config.apiUrl; // Should have apiUrl intellisense console.log('Typed container - User:', userName); console.log('Typed container - Config URL:', url); // Test non-registry key (should work with explicit typing) typedContainer.bind('CUSTOM_SERVICE').toValue('custom value'); const customValue = typedContainer.get('CUSTOM_SERVICE'); console.log('Typed container - Custom:', customValue); console.log('=== Testing Untyped Container ==='); // Test untyped container const untypedContainer = createContainer(); // Bind any keys untypedContainer.bind('ANY_SERVICE').toValue({ getData: () => 'data' }); untypedContainer.bind('RANDOM_VALUE').toValue(42); // Get requires explicit typing const anyService = untypedContainer.get('ANY_SERVICE'); const randomValue = untypedContainer.get('RANDOM_VALUE'); console.log('Untyped container - Data:', anyService.getData()); console.log('Untyped container - Value:', randomValue); console.log('=== All tests passed! ===');