Skip to content

Commit c398533

Browse files
Marcin Nowak-LiebiediewEric Swanson
andauthored
fix(typescript): add index.d.ts file for type safety when importing generated declarations (#2597)
* fix(typescript): add index.d.ts file for type safety when importing generated declarations * Update e2e/tests-dfx/generate.bash * fixes #2537 Co-authored-by: Eric Swanson <64809312+ericswanson-dfinity@users.noreply.github.com>
1 parent 6fb3f91 commit c398533

5 files changed

Lines changed: 58 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ It is now possible to set the http adapter's log level in dfx.json or in network
8888

8989
By default, a log level of "error" is used, in order to keep the output of a first-time `dfx start` minimal. Change it to "debug" for more verbose logging.
9090

91+
### fix(typescript): add index.d.ts file for type safety when importing generated declarations
92+
93+
Adds an index.d.ts file to the generated declarations, allowing for better type safety in TypeScript projects.
94+
9195
### chore: reduce verbosity of dfx start
9296

9397
`dfx start` produces a lot of log output that is at best irrelevant for most users.

docs/cli-reference/dfx-generate.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ The behavior of `dfx generate` is controlled by the `dfx.json` configuration fil
4343

4444
Outputs from `dfx generate`:
4545

46-
| Language | File |
47-
|------------------|-----------------------------------------|
48-
| `JavaScript(js)` | `index.js` and `<canister_name>.did.js` |
49-
| `TypeScript(ts)` | `<canister_name>.did.ts` |
50-
| `Candid(did)` | `<canister_name>.did` |
51-
| `Motoko(mo)` | `<canister_name>.mo` |
46+
| Language | File |
47+
|------------------|------------------------------------------|
48+
| `JavaScript(js)` | `index.js` and `<canister_name>.did.js` |
49+
| `TypeScript(ts)` | `index.d.ts` and `<canister_name>.did.ts`|
50+
| `Candid(did)` | `<canister_name>.did` |
51+
| `Motoko(mo)` | `<canister_name>.mo` |
5252

5353
## Examples
5454

e2e/tests-dfx/generate.bash

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bats
2+
3+
load ../utils/_
4+
5+
setup() {
6+
standard_setup
7+
8+
dfx_new hello
9+
}
10+
11+
teardown() {
12+
dfx_stop
13+
14+
standard_teardown
15+
}
16+
17+
@test "dfx generate creates files" {
18+
dfx_start
19+
dfx canister create --all
20+
dfx build
21+
dfx canister install --all
22+
23+
dfx --version
24+
dfx generate
25+
26+
assert_file_exists "src/declarations/hello_backend/hello_backend.did"
27+
assert_file_exists "src/declarations/hello_backend/hello_backend.did.js"
28+
assert_file_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
29+
assert_file_exists "src/declarations/hello_backend/index.js"
30+
assert_file_exists "src/declarations/hello_backend/index.d.ts"
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ActorSubclass, HttpAgentOptions, ActorConfig } from '@dfinity/agent';
2+
import { Principal } from '@dfinity/principal';
3+
4+
import { _SERVICE } from './{{canister_name}}.did';
5+
6+
export declare interface CreateActorOptions {
7+
agentOptions?: HttpAgentOptions;
8+
actorOptions?: ActorConfig;
9+
}
10+
11+
export declare const createActor: (
12+
canisterId: string | Principal,
13+
options: CreateActorOptions
14+
) => ActorSubclass<_SERVICE>;
15+
16+
export declare const {{canister_name}}: ActorSubclass<_SERVICE>;

src/dfx/src/lib/models/canister.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ fn build_canister_js(canister_id: &CanisterId, canister_info: &CanisterInfo) ->
596596
}
597597
// skip
598598
"index.js.hbs" => {}
599+
"index.d.ts.hbs" => {}
599600
_ => unreachable!(),
600601
}
601602
}

0 commit comments

Comments
 (0)