|
| 1 | +/** |
| 2 | + * Definite assignment assertion (!) test cases |
| 3 | + * Tests the TypeScript ! operator on property declarations to suppress |
| 4 | + * TypeScript's strict property initialization checking |
| 5 | + */ |
| 6 | + |
| 7 | +// Basic class with definite assignment assertion |
| 8 | +class BasicClass { |
| 9 | + // Definite assignment assertion - property will be assigned in constructor |
| 10 | + value!: string; |
| 11 | + |
| 12 | + // Another definite assignment |
| 13 | + count!: number; |
| 14 | + |
| 15 | + // Optional property for comparison (no assertion needed) |
| 16 | + optional?: boolean; |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.value = "initialized"; |
| 20 | + this.count = 42; |
| 21 | + // optional is not assigned - that's fine |
| 22 | + } |
| 23 | + |
| 24 | + updateValue(newValue: string) { |
| 25 | + this.value = newValue; |
| 26 | + } |
| 27 | + |
| 28 | + getValue(): string { |
| 29 | + return this.value; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// Interface for configuration (no definite assignment assertions in interfaces) |
| 34 | +interface Config { |
| 35 | + apiUrl: string; // Will be set by initialization method |
| 36 | + timeout: number; // Will be set by initialization method |
| 37 | + debug?: boolean; // Optional, may not be set |
| 38 | +} |
| 39 | + |
| 40 | +// Class implementing interface with assertions |
| 41 | +class ConfigManager implements Config { |
| 42 | + apiUrl!: string; |
| 43 | + timeout!: number; |
| 44 | + debug?: boolean; |
| 45 | + |
| 46 | + // Additional properties with assertions |
| 47 | + initialized!: boolean; |
| 48 | + settings!: { [key: string]: any }; |
| 49 | + |
| 50 | + initialize(url: string, timeoutMs: number) { |
| 51 | + this.apiUrl = url; |
| 52 | + this.timeout = timeoutMs; |
| 53 | + this.initialized = true; |
| 54 | + this.settings = { theme: "dark" }; |
| 55 | + } |
| 56 | + |
| 57 | + isReady(): boolean { |
| 58 | + return this.initialized; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Inheritance with definite assignment assertions |
| 63 | +abstract class BaseService { |
| 64 | + serviceName!: string; // Subclasses must set this |
| 65 | + version!: number; // Subclasses must set this |
| 66 | + |
| 67 | + abstract init(): void; |
| 68 | + |
| 69 | + getInfo(): string { |
| 70 | + return `${this.serviceName} v${this.version}`; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class UserService extends BaseService { |
| 75 | + userCount!: number; // Will be set in init |
| 76 | + |
| 77 | + init(): void { |
| 78 | + this.serviceName = "UserService"; |
| 79 | + this.version = 1; |
| 80 | + this.userCount = 0; |
| 81 | + } |
| 82 | + |
| 83 | + addUser(): void { |
| 84 | + this.userCount++; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Generic class with definite assignment assertions |
| 89 | +class Container<T> { |
| 90 | + data!: T; |
| 91 | + size!: number; |
| 92 | + capacity!: number; |
| 93 | + |
| 94 | + initialize(item: T, cap: number): void { |
| 95 | + this.data = item; |
| 96 | + this.size = 1; |
| 97 | + this.capacity = cap; |
| 98 | + } |
| 99 | + |
| 100 | + getData(): T { |
| 101 | + return this.data; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Static properties with definite assignment assertions |
| 106 | +class StaticExample { |
| 107 | + static globalConfig: { [key: string]: any }; // Will be initialized in setup() |
| 108 | + static initialized: boolean; // Will be initialized in setup() |
| 109 | + |
| 110 | + instanceData!: string; |
| 111 | + |
| 112 | + static setup(): void { |
| 113 | + StaticExample.globalConfig = { mode: "production" }; |
| 114 | + StaticExample.initialized = true; |
| 115 | + } |
| 116 | + |
| 117 | + constructor(data: string) { |
| 118 | + this.instanceData = data; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Mixed property types |
| 123 | +class MixedProperties { |
| 124 | + // Definite assignment assertions |
| 125 | + definiteString!: string; |
| 126 | + definiteNumber!: number; |
| 127 | + definiteBoolean!: boolean; |
| 128 | + definiteObject!: { id: number; name: string }; |
| 129 | + definiteArray!: string[]; |
| 130 | + |
| 131 | + // Optional properties |
| 132 | + optionalString?: string; |
| 133 | + optionalNumber?: number; |
| 134 | + |
| 135 | + // Regular properties with initializers |
| 136 | + regularString: string = "default"; |
| 137 | + regularNumber: number = 0; |
| 138 | + |
| 139 | + setup(): void { |
| 140 | + this.definiteString = "assigned"; |
| 141 | + this.definiteNumber = 123; |
| 142 | + this.definiteBoolean = true; |
| 143 | + this.definiteObject = { id: 1, name: "test" }; |
| 144 | + this.definiteArray = ["a", "b", "c"]; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// Readonly with definite assignment assertion |
| 149 | +class ReadonlyExample { |
| 150 | + readonly id!: number; // Will be set in constructor |
| 151 | + readonly name!: string; // Will be set in constructor |
| 152 | + readonly created!: Date; // Will be set in constructor |
| 153 | + |
| 154 | + constructor(id: number, name: string) { |
| 155 | + // TypeScript allows assignment in constructor even for readonly |
| 156 | + (this as any).id = id; |
| 157 | + (this as any).name = name; |
| 158 | + (this as any).created = new Date(); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// Main function to test all scenarios |
| 163 | +function main(): void { |
| 164 | + console.log("Definite assignment assertion test started"); |
| 165 | + |
| 166 | + // Test BasicClass |
| 167 | + const basic = new BasicClass(); |
| 168 | + console.log("Basic value:", basic.getValue()); |
| 169 | + basic.updateValue("updated"); |
| 170 | + console.log("Updated value:", basic.getValue()); |
| 171 | + |
| 172 | + // Test ConfigManager |
| 173 | + const config = new ConfigManager(); |
| 174 | + config.initialize("https://api.example.com", 5000); |
| 175 | + console.log("Config ready:", config.isReady()); |
| 176 | + console.log("API URL:", config.apiUrl); |
| 177 | + |
| 178 | + // Test UserService |
| 179 | + const userService = new UserService(); |
| 180 | + userService.init(); |
| 181 | + console.log("Service info:", userService.getInfo()); |
| 182 | + userService.addUser(); |
| 183 | + |
| 184 | + // Test Container |
| 185 | + const stringContainer = new Container<string>(); |
| 186 | + stringContainer.initialize("hello", 10); |
| 187 | + console.log("Container data:", stringContainer.getData()); |
| 188 | + |
| 189 | + // Test StaticExample |
| 190 | + StaticExample.setup(); |
| 191 | + const _staticExample = new StaticExample("instance"); |
| 192 | + console.log("Static initialized:", StaticExample.initialized); |
| 193 | + |
| 194 | + // Test MixedProperties |
| 195 | + const mixed = new MixedProperties(); |
| 196 | + mixed.setup(); |
| 197 | + console.log("Mixed string:", mixed.definiteString); |
| 198 | + console.log("Regular string:", mixed.regularString); |
| 199 | + |
| 200 | + // Test ReadonlyExample |
| 201 | + const readonly = new ReadonlyExample(42, "readonly test"); |
| 202 | + console.log("Readonly ID:", readonly.id); |
| 203 | + |
| 204 | + console.log("Definite assignment assertion test completed"); |
| 205 | +} |
| 206 | + |
| 207 | +main(); |
0 commit comments