(expr: TcbExpr, tcb: Context, originalValue: string | AST)
| 167 | * Potentially widens the type of `expr` according to the type-checking configuration. |
| 168 | */ |
| 169 | export function widenBinding(expr: TcbExpr, tcb: Context, originalValue: string | AST): TcbExpr { |
| 170 | if (!tcb.env.config.checkTypeOfInputBindings) { |
| 171 | // If checking the type of bindings is disabled, cast the resulting expression to 'any' |
| 172 | // before the assignment. |
| 173 | return new TcbExpr(`((${expr.print()}) as any)`); |
| 174 | } else if (!tcb.env.config.strictNullInputBindings) { |
| 175 | if (originalValue instanceof LiteralMap || originalValue instanceof LiteralArray) { |
| 176 | // Object literals and array literals should not be wrapped in non-null assertions as that |
| 177 | // would cause literals to be prematurely widened, resulting in type errors when assigning |
| 178 | // into a literal type. |
| 179 | return expr; |
| 180 | } else { |
| 181 | // If strict null checks are disabled, erase `null` and `undefined` from the type by |
| 182 | // wrapping the expression in a non-null assertion. |
| 183 | return new TcbExpr(`(${expr.print()})!`); |
| 184 | } |
| 185 | } else { |
| 186 | // No widening is requested, use the expression as is. |
| 187 | return expr; |
| 188 | } |
| 189 | } |
no test coverage detected