实现说明
各个操作都建立在顶层绑定之上:flags。
使用要点
- 跑完文件,输出里会留下标记——
anonymous:、beta checkout:、authenticated:。
常见问题
除了语言工具链还要下载什么吗?
除了这个语言的工具链,什么都不用再下载。 文件的状态由几个模块级绑定持有:flags。
核心语句
const shape: typeof limitsShape = checkedLimits;
const checkedLimits: RateLimits = limits;
const flags = featureFlags as Flags;
代码示例
// import attributes declare the module type explicitly.
// A wrong or missing type: 'json' is a compile-time error now.
import limits from './rate-limits.json' with { type: 'json' };
import featureFlags from './flags.json' with { type: 'json' };
interface RateLimits {
anonymous: number;
authenticated: number;
}
interface Flags {
betaCheckout: boolean;
}
const checkedLimits: RateLimits = limits;
console.log('anonymous:', checkedLimits.anonymous);
const flags = featureFlags as Flags;
console.log('beta checkout:', flags.betaCheckout);
// Type-only import reads the *inferred* shape of the JSON module for the
// compiler; the runtime build drops it entirely. Attributes are runtime
// syntax, so a type-only import of the same JSON goes without them.
import type limitsShape from './rate-limits.json';
const shape: typeof limitsShape = checkedLimits;
console.log('authenticated:', shape.authenticated);