Type-check the ports passed to `getMappedPort`
Maintainers usually reply within 4 days
@cschmertz is already working on this.
Since Oct 27, 2026.
Assessment
- Difficulty
- 4/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- docker, node.js, typescript
Research direction
Start by reading withExposedPorts and start in packages/testcontainers/src/test-container.ts and packages/testcontainers/src/generic-container/generic-container.ts, then inspect getMappedPort on StartedTestContainer. Check the repository’s existing TypeScript type tests and add cases for literal exposed ports, UDP ports, repeated calls, and dynamic ports. Done means exposed literal ports are type-checked while dynamic ports retain current behavior; coordinate the interface changes with #1529 and #1563.
Written by the indexing model from the issue text.
Description
What happens
getMappedPort accepts any number. Asking for a port that was never exposed compiles, and fails when the test runs:
const container = await new GenericContainer(image).withExposedPorts(8080).start();
container.getMappedPort(8081); // compiles
// Error: No port binding found for :8081/tcp
The mistake is usually a typo, or a port constant that drifted from the one passed to withExposedPorts.
Expected
The ports given to withExposedPorts are carried in the builder's type, and getMappedPort on the started container accepts only those:
const container = await new GenericContainer(image).withExposedPorts(8080, "53/udp").start();
container.getMappedPort(8080);
container.getMappedPort(53, "udp");
container.getMappedPort(8081); // compile error
Where the ports are not known statically, nothing changes: getMappedPort accepts any number, as it does today.
Where
withExposedPortsandstartinpackages/testcontainers/src/test-container.tsandpackages/testcontainers/src/generic-container/generic-container.tsgetMappedPortonStartedTestContainer
Notes
A prototype of the types compiles under the repo's TypeScript (7.0.2) with these cases checked. It needs no class type parameter, so the fluent this return type and module subclasses are unaffected:
declare const exposed: unique symbol;
type Exposed<P extends number> = { readonly [exposed]: { readonly [K in P]: true } };
type PortsOf<T> = T extends { readonly [exposed]: infer M } ? keyof M & number : number;
interface StartedTestContainer<P extends number = number> {
getMappedPort(port: P, protocol?: string): number;
getMappedPort(portWithProtocol: `${P}/${"tcp" | "udp"}`): number;
}
class GenericContainer {
withExposedPorts<const T extends PortWithOptionalBinding[]>(...ports: T): this & Exposed<ContainerPort<T[number]>>;
start(): Promise<StartedTestContainer<PortsOf<this>>>;
}
- Each
withExposedPortscall intersects a marker ontothis. Laterwith*calls returnthis, so the marker survives the rest of the chain, and repeated calls accumulate. - It falls back to
number, and so to today's behaviour, when a port is anumbervariable, when the builder is stored in a variable typedGenericContainer, and whenwithExposedPortswas never called. - Module containers are unaffected, because their
start()returns their own started type. A module could opt in later. - Ports that a subclass exposes in its constructor or hooks are not visible to the caller's type.
- The
consttype parameter needs TypeScript 5.0 or later in consuming projects. Without it, only the object form ({ container, host }) loses precision. - The intersection shows up in editor hovers, for example
GenericContainer & Exposed<8080 | 53>. - Changing the signature on the
TestContainerinterface is a compile error for anyone who implements that interface without extendingGenericContainer.
Dependencies
- Nothing blocks this. It can start now.
- Coordinate with #1529: both change the
TestContainerinterface. - Coordinate with #1563: the started type gains a type parameter, which the forwarding wrapper has to pass through until it is removed.
- Dominant language
- TypeScript
- Stars
- 2.7k
- Forks
- 290
- Avg merge
- 4d 1h
- Merged PRs (30d)
- 22
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from testcontainers/testcontainers-node
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
testcontainers/testcontainers-node#1673 ·
Maintainers usually reply within 4 days
-
enhancement minor
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
testcontainers/testcontainers-node#1539 ·
Maintainers usually reply within 4 days
-
Let `Wait.forListeningPorts()` wait on specific portsPossibly taken @artbom claimed this 29 days ago. Openenhancement minor
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
testcontainers/testcontainers-node#1533 ·
Maintainers usually reply within 4 days
-
enhancement minor
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
testcontainers/testcontainers-node#1531 ·
Maintainers usually reply within 4 days
-
Export the types used in public method signaturesPossibly taken @artbom claimed this 29 days ago. Openenhancement minor
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
testcontainers/testcontainers-node#1528 ·
Maintainers usually reply within 4 days
All issues in testcontainers/testcontainers-node
Similar issues
-
prio:low scope:hud
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
OliverRomhagen/vrigstad-joyride#953 ·
Maintainers usually reply within 1 day
-
review-followup
Difficulty 1/5 Under an hour Newbie friendliness 92/100
griffinwork40/agent-afk#5446 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
kacper-szewczyk/Ticknest#346 ·
Maintainers usually reply within 1 day
-
echarts geo: a GeoJSON URL that returns 200 with a body that is not JSON is fetched on each refreshOpenbug plugin/echarts/geo
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Yeachan-Heo/gajae-code#8230 ·
Maintainers usually reply within 1 day