This error occurs when TypeScript's type system prevents you from passing a 'never' type value to a function parameter, even if that parameter accepts 'any'. The 'never' type represents an impossible value, and TypeScript's variance rules prevent it from being assigned to parameters.
The 'never' type is TypeScript's bottom type—a type that represents an impossible or unreachable state. It's the only type that has no values. When you encounter this error, it means you're trying to pass a value inferred as 'never' to a function parameter. Even though 'any' is permissive in TypeScript's type system, there's a strict rule: 'never' cannot be assigned to 'any' in function parameters due to contravariance. Function parameters are contravariant, meaning they enforce stricter rules on input types. This is a deliberate design choice that prevents you from accidentally passing impossible values where actual values are expected. This error typically indicates a logic problem in your code—you have code that should be unreachable or a type inference issue that needs fixing.
When you declare an empty array without a type annotation, TypeScript infers it as never[]. Explicitly type the array:
// Error: Argument of type 'never' is not assignable to parameter of type 'any'
const items = [];
process.stdout.write(items[0]);
// Fix: Explicitly type the array
const items: string[] = [];
process.stdout.write(items[0] || '');This is the most common cause. Always provide explicit types for empty arrays, especially when they'll be used in function calls.
If you're using if/else or switch statements to narrow types, ensure the remaining type isn't 'never':
function process(value: string | number) {
if (typeof value === 'string') {
console.log(value.toUpperCase());
} else if (typeof value === 'number') {
console.log(value.toFixed(2));
} else {
// Here, 'value' is typed as 'never' because all cases are handled
// Don't pass 'value' to functions expecting 'any'
someFunction(value); // Error!
}
}
// Fix: Ensure the else clause is truly unreachable, or rethink the logic
function process(value: string | number) {
if (typeof value === 'string') {
console.log(value.toUpperCase());
} else {
// 'value' is number here
console.log(value.toFixed(2));
}
}If you're using generics that can collapse to 'never', provide explicit type constraints:
// This might infer 'never' in some cases
function handler<T>(value: T) {
console.log(value);
}
// If called with incompatible types, T becomes 'never'
// Instead, use explicit typing
const result: string = someGenericFunc<string>();
// Or provide a fallback/default type
function handler<T = unknown>(value: T) {
console.log(value);
}When filtering or transforming data, ensure TypeScript doesn't infer 'never':
// Problematic: TypeScript might infer 'never' for filtered values
const values = [1, 2, 3];
const filtered = values.filter(v => v > 10); // Empty array inferred as 'never[]'
// Fix: Explicitly type the result
const filtered: number[] = values.filter(v => v > 10);
// Or use a type guard function
function isValid(v: unknown): v is string {
return typeof v === 'string';
}
const strs = values.filter(isValid); // Correctly inferredIf you've confirmed your logic is correct and this is a legitimate use case, you can use a type assertion:
const value: never = somethingImpossible();
// Only use as a last resort
someFunction(value as any);
// Better: Question why you have a 'never' type in the first place
// This usually indicates a logic errorType assertions should be rare. If you're using them frequently for this error, reconsider your type design.
This error relates to TypeScript's contravariance principle for function parameters. When you assign a function to another function type, the parameter types must be "larger" or more general than the original. Since 'never' is the smallest possible type (the bottom type), it fails contravariance checks.
Understanding this helps you see why the error exists: if a function accepts 'never' parameters, it's saying "I can only be called with impossible values," which is useful for exhaustiveness checking but means the function can never actually be called in practice.
In strict mode, TypeScript enforces this rule rigorously. If you're using 'any' to bypass type checking, consider whether your type design needs refinement instead.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript