swift-syntax 扩展,用于基于字面量信息从表达式推断类型。
实现为 ExprSyntaxProtocol
的扩展。
从字面量信息推断的类型可以从 inferredType
属性获取。
import SwiftSyntax
import SwiftSyntaxBuilder
import LiteralTypeInference
let expr: ExprSyntax = """
[
"item1": [
(1, 2.0, "3", true),
(1.0, 2, "7", false),
(nil, nil, nil, false)
],
"item2": [],
"item3": [
(1, 2, "3", true)
]
]
"""
// Get the type of expr from the `inferredType` property
let type = expr.inferredType
print(type?.formatted().description)
// => [String: [(Double?, Double?, String?, Bool)]]
字面量类型推断根据 MIT 许可证发布。参见 LICENSE