Simple Neural Network Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Simple Neural Network runs in O(n*w) time and O(w) space, in TypeScript as in every other language.
- ✓The same runtime as JavaScript, Math.random with no seed and no Gaussian distribution, but the value is in typing the network's shape: a Layer interface with weights: Float64Array, biases: Float64Array, and activation: 'sigmoid' | 'relu' | 'tanh' as a literal union rather than a bare string.
- ✓Declare the architecture once as a typed, as const array of layer sizes and derive the Layer[] array from it with a single loop, so there is exactly one place the shape is written down; separately hand-writing the sizes AND separately writing out new Float64Array(30) literals in two places is what a reviewer would flag, because it defeats the entire point of having the type layer describe the architecture.
- ✓The TypeScript trap: Declaring Layer.activation as a plain string instead of the literal union 'sigmoid' | 'relu' | 'tanh' compiles fine and then silently accepts a typo like 'sigmiod' at a call site with zero warning, because a bare string is a structural superset of every literal you meant to allow.
- ✓Identical to JavaScript at runtime; the only TypeScript-specific cost is at build time, and it scales with how cleverly the architecture is encoded.
What Simple Neural Network actually is
Simple Neural Network in TypeScript: what you actually reach for
TypeScript implementation
type Activation = 'sigmoid' | 'relu' | 'tanh';
interface Layer {
weights: Float64Array;
biases: Float64Array;
inSize: number;
outSize: number;
activation: Activation; // union, not `string` - a typo fails to compile
}
const ARCHITECTURE = [784, 128, 10] as const; // one place the shape lives
function buildNetwork(sizes: readonly number[], activation: Activation, rng: () => number): Layer[] {
const layers: Layer[] = [];
for (let i = 0; i < sizes.length - 1; i += 1) {
const inSize = sizes[i], outSize = sizes[i + 1];
const weights = new Float64Array(inSize * outSize);
const scale = Math.sqrt(2 / inSize);
for (let k = 0; k < weights.length; k += 1) weights[k] = (rng() * 2 - 1) * scale;
layers.push({ weights, biases: new Float64Array(outSize), inSize, outSize, activation });
}
return layers;
}
function applyActivation(z: number, kind: Activation): number {
switch (kind) {
case 'sigmoid': return 1 / (1 + Math.exp(-z));
case 'relu': return Math.max(0, z);
case 'tanh': return Math.tanh(z);
// no default: the union is exhaustive, so an added activation forces every switch to update
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(n*w)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Simple Neural Network?
The same runtime as JavaScript, Math.random with no seed and no Gaussian distribution, but the value is in typing the network's shape: a Layer interface with weights: Float64Array, biases: Float64Array, and activation: 'sigmoid' | 'relu' | 'tanh' as a literal union rather than a bare string.
What is the most common TypeScript mistake when implementing Simple Neural Network?
Declaring Layer.activation as a plain string instead of the literal union 'sigmoid' | 'relu' | 'tanh' compiles fine and then silently accepts a typo like 'sigmiod' at a call site with zero warning, because a bare string is a structural superset of every literal you meant to allow. The forward pass's switch on activation then falls through with no case matching, off the end of the function with no return statement, so the call implicitly returns undefined rather than a number -- which then silently propagates as NaN through every downstream calculation instead of throwing. The fix is declaring the union type on the interface field itself, not adding a check inside the switch body after the fact.
How does Simple Neural Network actually perform in TypeScript?
Identical to JavaScript at runtime; the only TypeScript-specific cost is at build time, and it scales with how cleverly the architecture is encoded. A NetworkConfig typed as readonly number[] costs tsc nothing, but encoding the actual per-layer array LENGTHS as tuple types, so a shape mismatch becomes a compile error instead of a runtime NaN, is exactly the kind of deep generic and conditional-type machinery that measurably slows tsc on a nontrivial architecture, the same tradeoff the backpropagation page's Layer<In, Out> caveat describes. Time complexity is O(n*w) and space is O(w).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialWant to try this with a model on your own laptop? Local AI setup guides for Ollama and LM Studio are on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.