After testing various scenarios, the comment by @jcalz helped to pass all test cases, where if we add type as number or null.

const var1: number | null = null;
    
function test(param1:number | null){
   console.log(param1);
}
    
test(var1);
Answer from Raviraj Gardi on Stack Overflow
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
Optional chaining is a JavaScript feature that works well with TypeScript's null handling. It allows accessing properties on an object that may or may not exist, using compact syntax. It can be used with the ?. operator when accessing properties. interface House { sqft: number; yard?: { sqft: number; }; } function printYardSize(house: House) { const yardSize = house.yard?.sqft; if (yardSize === undefined) { console.log('No yard'); } else { console.log(`Yard is ${yardSize} sqft`); } } let home: House = { sqft: 500 }; printYardSize(home); // Prints 'No yard' Try it Yourself »
Discussions

Maybe<number> null vs undefined
null and undefined are not the same thing. ? Implies that the property may not at all be on that object, or could be undefined. | null means the property will always exist, but could be null. More on reddit.com
🌐 r/typescript
13
3
January 26, 2023
How to declare a type as nullable in TypeScript? - Stack Overflow
I have an interface in TypeScript. interface Employee{ id: number; name: string; salary: number; } I would like to make salary as a nullable field (Like we can do in C#). More on stackoverflow.com
🌐 stackoverflow.com
Allow null to be used like a number in typescript - Stack Overflow
I am wondering if there is an option that allows null to be used like a 0 in typescripts compiler options. I know I can cast null to a number by doing Number(null), but I would prefer just being ab... More on stackoverflow.com
🌐 stackoverflow.com
Docs: [strict-boolean-expressions] nullable number as wrong 'undefined' type
Before You File a Documentation Request Please Confirm You Have Done The Following... I have looked for existing open or closed documentation requests that match my proposal. I have read the FAQ an... More on github.com
🌐 github.com
10
September 2, 2022
🌐
Medium
greg-pabian.medium.com › nullability-in-typescript-60be8c5a6d87
Nullability in TypeScript - Greg Pabian - Medium
May 17, 2019 - TypeScript defines a special type ... but I am goint to present you with one interesting usage of it. ... type NullableNumber = number | null | undefined;const nullableNumbers: ReadonlyArray<NullableNumber> = [1, 2, 3, null, ...
🌐
Reddit
reddit.com › r/typescript › maybe null vs undefined
r/typescript on Reddit: Maybe<number> null vs undefined
January 26, 2023 -

Maybe our GraphQL schema is bad, I don't know. I just work on the frontend.

I'm having problems like this when I turn strict non-null checks on:

Type 'Maybe<number>' is not assignable to type 'number | undefined'.
  Type 'null' is not assignable to type 'number | undefined'.ts(2322)

Variable that I have is from GraphQL codegen: Maybe<number>

I want to put it into here?: number;

Can I do something graphQL codegen settings? Preferably.

I can't play with the schema itself, not easily.

I can't change the whole app and turn everything into here?: number | null; that is too verbose and feels stupid, there must be a better way.

🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › typescript tutorial › typescript nullable
TypeScript Nullable | Rules and Regulations for Nullable with Examples
April 11, 2023 - Type ‘null’ is not assignable to type ‘number’: This is for type z assigned as null. With this, we conclude our topic ‘TypeScript Nullable’. We have seen what Nullables are and how are they defined. Using of -strictNullChecks flag to be ON or OFF, based on which Null or undefined variables are bypassed.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › null in typescript
Null in TypeScript - Tektutorialshub
March 15, 2023 - The value null represents the intentional absence of any object value. It represents nothing or no value. null means we know that the variable does not have any value. TypeScript does not set the value of a variable to null.
🌐
typescriptlang.org
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
There won’t be an exception or null generated if the type assertion is wrong. TypeScript only allows type assertions which convert to a more specific or less specific version of a type. This rule prevents “impossible” coercions like: ... x = "hello" as number;Conversion of type 'string' ...
🌐
This Dot Labs
thisdot.co › blog › avoiding-null-and-undefined-with-nonnullable-type
Avoiding Null and Undefined with NonNullable<T> in TypeScript - This Dot Labs
June 10, 2023 - To initialize the properties with null, the constructor uses the nullish coalescing operator, ??. When creating a new Person, you can pass null or undefined as arguments, and the class will interpret them as null. Understanding and correctly implementing conditional types like NonNullable<T> in TypeScript is crucial to reduce potential code pitfalls.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
TypeScript for Beginners, Part 2: Basic Data Types | Envato Tuts+
January 28, 2022 - Just as in JavaScript, the null data type in TypeScript can have only one valid value: null. A null variable cannot contain other data types like number and string.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-can-number-be-null
Handling Nullable Numbers in TypeScript
In the doubleNumber function above, we check if the input number is null before doubling its value. If the input is null, we return null to indicate that the result is also null. Type guards in TypeScript provide a way to narrow down the type of a variable within a certain block of code.
🌐
GeeksforGeeks
geeksforgeeks.org › explain-the-concept-of-null-and-its-uses-in-typescript
Explain the concept of null and its uses in TypeScript - GeeksforGeeks
March 2, 2022 - Null refers to a value that is either empty or a value that doesn't exist. It's on purpose that there's no value here. TypeScript does not make a variable null by default. By default unassigned variables or variables which are declared without ...
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › null vs undefined in typescript
Null Vs Undefined in TypeScript - Tektutorialshub
March 15, 2023 - The value null indicates that you know that the field does not have a value. It is an intentional absence of value. Whenever we declare a variable without initializing it with a value, TypeScript initializes it as undefined.
🌐
xjavascript
xjavascript.com › blog › typescript-check-if-number-is-not-null-or-undefined
TypeScript: Checking if a Number is Not Null or Undefined — xjavascript.com
In TypeScript, null and undefined are primitive types. null represents the intentional absence of any object value, while undefined typically means a variable has been declared but not assigned a value, or a function has no return value.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-string-to-number-null
Converting TypeScript String to Number and Handling Null Values
type NumberOrString<T> = T extends number ? never : string; const str = '123'; if (str) { if (Number(str)) { const num = parseFloat(str); console.log(num); // Output: 123 } else { console.log('Value is not a number'); } } else { console.log('Value is null or undefined'); } In this blog post, we've explored the process of converting TypeScript string values to numbers and handling null values in your code.
🌐
DEV Community
dev.to › thinkster › 2-methods-for-dealing-with-nulls-in-typescript-4kn0
2 Methods for Dealing with Nulls in TypeScript - DEV Community
September 10, 2020 - The above code would throw an error with the strict null check. You're creating a user, and initializing it to null, but not declaring it as a nullable type. In order to do that, and make it possibly be nullable, use the union operator like so: This won't throw a TypeScript error.
🌐
xjavascript
xjavascript.com › blog › typescript-null
Understanding and Working with `null` in TypeScript — xjavascript.com
TypeScript has a compiler option called strictNullChecks. When this flag is enabled (which is recommended for most projects), the compiler will enforce stricter rules regarding null and undefined values.