Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ It is now possible to set the http adapter's log level in dfx.json or in network

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.

### fix(typescript): add index.d.ts file for type safety when importing generated declarations

Adds an index.d.ts file to the generated declarations, allowing for better type safety in TypeScript projects.

### chore: reduce verbosity of dfx start

`dfx start` produces a lot of log output that is at best irrelevant for most users.
Expand Down
12 changes: 6 additions & 6 deletions docs/cli-reference/dfx-generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ The behavior of `dfx generate` is controlled by the `dfx.json` configuration fil

Outputs from `dfx generate`:

| Language | File |
|------------------|-----------------------------------------|
| `JavaScript(js)` | `index.js` and `<canister_name>.did.js` |
| `TypeScript(ts)` | `<canister_name>.did.ts` |
| `Candid(did)` | `<canister_name>.did` |
| `Motoko(mo)` | `<canister_name>.mo` |
| Language | File |
|------------------|------------------------------------------|
| `JavaScript(js)` | `index.js` and `<canister_name>.did.js` |
| `TypeScript(ts)` | `index.d.ts` and `<canister_name>.did.ts`|
| `Candid(did)` | `<canister_name>.did` |
| `Motoko(mo)` | `<canister_name>.mo` |

## Examples

Expand Down
31 changes: 31 additions & 0 deletions e2e/tests-dfx/generate.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats

load ../utils/_

setup() {
standard_setup

dfx_new hello
}

teardown() {
dfx_stop

standard_teardown
}

@test "dfx generate creates files" {
dfx_start
dfx canister create --all
dfx build
dfx canister install --all

dfx --version
dfx generate

assert_file_exists "src/declarations/hello_backend/hello_backend.did"
assert_file_exists "src/declarations/hello_backend/hello_backend.did.js"
assert_file_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
assert_file_exists "src/declarations/hello_backend/index.js"
assert_file_exists "src/declarations/hello_backend/index.d.ts"
}
16 changes: 16 additions & 0 deletions src/dfx/assets/language_bindings/index.d.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ActorSubclass, HttpAgentOptions, ActorConfig } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';

import { _SERVICE } from './{{canister_name}}.did';

export declare interface CreateActorOptions {
agentOptions?: HttpAgentOptions;
actorOptions?: ActorConfig;
}

export declare const createActor: (
canisterId: string | Principal,
options: CreateActorOptions
) => ActorSubclass<_SERVICE>;

export declare const {{canister_name}}: ActorSubclass<_SERVICE>;
1 change: 1 addition & 0 deletions src/dfx/src/lib/models/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ fn build_canister_js(canister_id: &CanisterId, canister_info: &CanisterInfo) ->
}
// skip
"index.js.hbs" => {}
"index.d.ts.hbs" => {}
_ => unreachable!(),
}
}
Expand Down