Skip to content

Commit 4edd9e7

Browse files
committed
Update: add import as const feature
1 parent e2f717d commit 4edd9e7

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@ And then add this to `tsconfig.json`.
2020
```
2121

2222
If you're using VSCode, [switch to workspace version](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript).
23+
24+
## `.const.toml`
25+
26+
If the file name ends with `.const.toml`, the typings will become as if you are using `as const`.
27+
28+
For example it will be like below.
29+
30+
`test.toml` and `test.const.toml`
31+
```toml
32+
key = 'val'
33+
```
34+
35+
`index.ts`
36+
```ts
37+
import test from './test.toml'
38+
import testConst from './test.const.toml'
39+
40+
type Test = typeof test // { key: string }
41+
type TestConst = typeof testConst // { readonly key: 'val' }
42+
```

src/createDts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Logger } from './logger'
55
export const createDtsSnapshot = (
66
tsModule: typeof ts,
77
scriptSnapshot: ts.IScriptSnapshot,
8-
logger: Logger
8+
logger: Logger,
9+
useAsConst: boolean
910
): ts.IScriptSnapshot => {
1011
const text = scriptSnapshot.getText(0, scriptSnapshot.getLength())
1112

@@ -20,7 +21,7 @@ export const createDtsSnapshot = (
2021
const data = toml.parse(text)
2122

2223
dts = `
23-
declare const data = ${JSON.stringify(data)}
24+
declare const data = ${JSON.stringify(data)}${useAsConst ? ' as const' : ''}
2425
export default data
2526
`
2627
} catch (e) {

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as ts from 'typescript/lib/tsserverlibrary'
22
import path from 'path'
33
import { createDtsSnapshot } from './createDts'
4-
import { isToml } from './util'
4+
import { isToml, isConstToml } from './util'
55
import { createLogger } from './logger'
66

77
function init({
@@ -21,7 +21,12 @@ function init({
2121
): ts.SourceFile => {
2222
if (isToml(fileName)) {
2323
logger.log(`create ${fileName}`)
24-
scriptSnapshot = createDtsSnapshot(tsModule, scriptSnapshot, logger)
24+
scriptSnapshot = createDtsSnapshot(
25+
tsModule,
26+
scriptSnapshot,
27+
logger,
28+
isConstToml(fileName)
29+
)
2530
}
2631
const sourceFile = _createLanguageServiceSourceFile(
2732
fileName,
@@ -43,7 +48,12 @@ function init({
4348
): ts.SourceFile => {
4449
if (isToml(sourceFile.fileName)) {
4550
logger.log(`update ${sourceFile.fileName}`)
46-
scriptSnapshot = createDtsSnapshot(tsModule, scriptSnapshot, logger)
51+
scriptSnapshot = createDtsSnapshot(
52+
tsModule,
53+
scriptSnapshot,
54+
logger,
55+
isConstToml(sourceFile.fileName)
56+
)
4757
}
4858
sourceFile = _updateLanguageServiceSourceFile(
4959
sourceFile,

src/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const isToml = (filename: string): boolean => filename.endsWith('.toml')
2+
export const isConstToml = (filename: string): boolean =>
3+
filename.endsWith('.const.toml')

0 commit comments

Comments
 (0)