|
| 1 | +//// [tests/cases/compiler/jsonImportConstAttribute.ts] //// |
| 2 | + |
| 3 | +=== /config.json === |
| 4 | +{ |
| 5 | + "appLocales": ["FR", "BE"], |
| 6 | +>"appLocales" : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 7 | + |
| 8 | + "enabled": true, |
| 9 | +>"enabled" : Symbol("enabled", Decl(config.json, 1, 31)) |
| 10 | + |
| 11 | + "count": 42 |
| 12 | +>"count" : Symbol("count", Decl(config.json, 2, 20)) |
| 13 | +} |
| 14 | + |
| 15 | +=== /main.ts === |
| 16 | +// Without const attribute - types should be widened |
| 17 | +import config from "./config.json" with { type: "json" }; |
| 18 | +>config : Symbol(config, Decl(main.ts, 1, 6)) |
| 19 | + |
| 20 | +// With const attribute - types should preserve literal types |
| 21 | +import constConfig from "./config.json" with { type: "json", const: "true" }; |
| 22 | +>constConfig : Symbol(constConfig, Decl(main.ts, 4, 6)) |
| 23 | + |
| 24 | +// Test widened types (without const) |
| 25 | +type Locales = typeof config.appLocales; // Should be string[] |
| 26 | +>Locales : Symbol(Locales, Decl(main.ts, 4, 77)) |
| 27 | +>config.appLocales : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 28 | +>config : Symbol(config, Decl(main.ts, 1, 6)) |
| 29 | +>appLocales : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 30 | + |
| 31 | +type Enabled = typeof config.enabled; // Should be boolean |
| 32 | +>Enabled : Symbol(Enabled, Decl(main.ts, 7, 40)) |
| 33 | +>config.enabled : Symbol("enabled", Decl(config.json, 1, 31)) |
| 34 | +>config : Symbol(config, Decl(main.ts, 1, 6)) |
| 35 | +>enabled : Symbol("enabled", Decl(config.json, 1, 31)) |
| 36 | + |
| 37 | +type Count = typeof config.count; // Should be number |
| 38 | +>Count : Symbol(Count, Decl(main.ts, 8, 37)) |
| 39 | +>config.count : Symbol("count", Decl(config.json, 2, 20)) |
| 40 | +>config : Symbol(config, Decl(main.ts, 1, 6)) |
| 41 | +>count : Symbol("count", Decl(config.json, 2, 20)) |
| 42 | + |
| 43 | +// Test literal types (with const) |
| 44 | +type ConstLocales = typeof constConfig.appLocales; // Should be readonly ["FR", "BE"] |
| 45 | +>ConstLocales : Symbol(ConstLocales, Decl(main.ts, 9, 33)) |
| 46 | +>constConfig.appLocales : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 47 | +>constConfig : Symbol(constConfig, Decl(main.ts, 4, 6)) |
| 48 | +>appLocales : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 49 | + |
| 50 | +type ConstEnabled = typeof constConfig.enabled; // Should be true |
| 51 | +>ConstEnabled : Symbol(ConstEnabled, Decl(main.ts, 12, 50)) |
| 52 | +>constConfig.enabled : Symbol("enabled", Decl(config.json, 1, 31)) |
| 53 | +>constConfig : Symbol(constConfig, Decl(main.ts, 4, 6)) |
| 54 | +>enabled : Symbol("enabled", Decl(config.json, 1, 31)) |
| 55 | + |
| 56 | +type ConstCount = typeof constConfig.count; // Should be 42 |
| 57 | +>ConstCount : Symbol(ConstCount, Decl(main.ts, 13, 47)) |
| 58 | +>constConfig.count : Symbol("count", Decl(config.json, 2, 20)) |
| 59 | +>constConfig : Symbol(constConfig, Decl(main.ts, 4, 6)) |
| 60 | +>count : Symbol("count", Decl(config.json, 2, 20)) |
| 61 | + |
| 62 | +// Verify the types work as expected |
| 63 | +const locale: "FR" | "BE" = constConfig.appLocales[0]; // Should work with const |
| 64 | +>locale : Symbol(locale, Decl(main.ts, 17, 5)) |
| 65 | +>constConfig.appLocales : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 66 | +>constConfig : Symbol(constConfig, Decl(main.ts, 4, 6)) |
| 67 | +>appLocales : Symbol("appLocales", Decl(config.json, 0, 1)) |
| 68 | +>0 : Symbol(0) |
| 69 | + |
| 70 | +const enabledTrue: true = constConfig.enabled; // Should work with const |
| 71 | +>enabledTrue : Symbol(enabledTrue, Decl(main.ts, 18, 5)) |
| 72 | +>constConfig.enabled : Symbol("enabled", Decl(config.json, 1, 31)) |
| 73 | +>constConfig : Symbol(constConfig, Decl(main.ts, 4, 6)) |
| 74 | +>enabled : Symbol("enabled", Decl(config.json, 1, 31)) |
| 75 | + |
| 76 | +// These should error without const (uncomment to verify errors) |
| 77 | +// const localeError: "FR" | "BE" = config.appLocales[0]; // Error: string not assignable to "FR" | "BE" |
| 78 | +// const enabledError: true = config.enabled; // Error: boolean not assignable to true |
| 79 | + |
| 80 | + |
0 commit comments