192 questions
Tooling
0
votes
2
replies
59
views
using optional chaining with === in case of both 'undefined'
I am using optional chaining quite often, but so far never encounter that issue, until today:
if both items can be undefined, rather don't use optional chaining. Only last test (below) can help in ...
0
votes
0
answers
77
views
In React + TypeScript, when should a prop be prop?: Type vs prop: Type | undefined? [duplicate]
When designing TypeScript props for React components,
I'm sometimes unsure whether I should type props like:
prop?: Type
or
prop: Type | undefined
My understanding so far:
prop?: Type
The prop ...
1
vote
1
answer
162
views
non-removable extra whitespace formatting after a colon, which creates an error in VSCode [closed]
I have this problem - I cannot use Tailwind in my project or optional chaining operator (?.) because VSCode adds an extra whitespace after a colon or after a dot. In case of taiwind, it adds a ...
0
votes
1
answer
78
views
TypeScript complains about optional chaining
I am using TypeScript 5.7.3.
Installed @types/[email protected].
TypeScript config:
{
"compilerOptions": {
"target": "ES2020",
"moduleResolution": &...
4
votes
2
answers
258
views
Angular typescript error with BigInt and Optional Chaining, nullish coalescing
We have an application that needs to be supported in old Chrome (v79).
Angular Version 13.2.7
Typescript Version 4.5.5
Node Version 16.14.0
We are getting an error:
ERROR: Big integer literals are not ...
1
vote
2
answers
96
views
How can I log a value in a Java Optional.of chain? [duplicate]
I'm using an Optional.of chain in Java to get a value,
Integer yearOfBirth = Optional.ofNullable(employee)
.map(Employee::getDateOfBirth)
....
-6
votes
1
answer
164
views
$_POST variable from request contains optional instead of string in PHP [closed]
The use case is a user/client sends a request to a server running PHP. I am trying to write the PHP code on the server e.g. an API endpoint to process the request.
I have existing production PHP code ...
8
votes
6
answers
2k
views
C++ equivalent of Javascript's '?.' operator for optional-chaining?
In Javascript, if I have a potentially null object obj, which, if not null, will have a field x - I can write obj?.x. This is called "optional chaining" or "safe navigation": If ...
0
votes
1
answer
392
views
How to implement Optional chaining in ngModel using angular 18
I have scenario in my project that a common shared service having a common variable (object) to store nested objects. I have to access and update its value in html using NgModel. Optional-chaining ...
0
votes
1
answer
121
views
Using nullable objects in Optional methods after covering it with ofNullable
I came across the below code:
public void method(MyObject obj) {
String value = Optional.ofNullable(obj)
.map(x -> obj.doSomething())
....
0
votes
1
answer
174
views
Optional's orElse() executed even when object is present [duplicate]
I've encountered an unexpected behavior while using the Optional API in Java.
Session session = sessionService
.getSessionById(id)
.orElse(
...
0
votes
1
answer
172
views
VSCode IntelliSense displaying an error on Optional Chaining in TypeScript
In VSCode, IntelliSense is saying a property does not exist on an object in a TypeScript file even when I specify the optional chaining syntax to short-circuit the error. The code compiles correctly ...
1
vote
1
answer
88
views
How to turn a optional of an string array into a optional of a string?
I struggle with finding an elegant way to convert a variable of type Optional<String[]> to Optional<String> and joining all elements of the given array.
Is there an elegant solution for ...
0
votes
0
answers
138
views
Is there a shorter way to tell Mypy that a given optional chaining is fine?
I am working on Python code where the domain logic makes it natural to have a class with an optional field of a second class, which itself has an optional field of a third class. Boiling it down to a ...
2
votes
2
answers
256
views
Why does optional-chaining multiple property accesses throw for an empty object, but not for null?
in a node repl or browser console:
> ({})?.a
undefined
> (null)?.a
undefined
> (null)?.a.b
undefined
> ({})?.a.b
Uncaught TypeError: Cannot read properties of undefined (reading 'b')
if (...
-2
votes
1
answer
61
views
Why do I get a null here?
So this piece of code
public boolean getBoolean(Integer Id) {
return Optional.of(Store.getMagazin(Id))
.map(MagazinDO::getBoolean)
.orElse(Boolean.FALSE);}
returns a null while ...
1
vote
2
answers
662
views
SyntaxError: Unexpected token '.' for optional chaining and Nullish coalescing [duplicate]
The issue was encountered after installing the aws s3 sdk.
I followed the suggested solutions from all other similar problems in the following manner:
-updated node version (even though I had version ...
4
votes
1
answer
109
views
Difference between raw and parameterized Optional when invoking orElseGet(Supplier)
Consider the following code:
public class Main {
public static void main(String[] args)
// throws Exception // line 1
{
Optional empty1 = Optional.empty(); // line 2
...
0
votes
1
answer
759
views
React query and optional chaining async await function
I don't think I understand how optional chaining affects the async/await functions.
I have a simple function that returns the user from Firebase Auth.
export async function getCurrentUser() {
const ...
2
votes
1
answer
325
views
Adding Optionals/null safety to a nested flatMap/stream
I'm having trouble making my method null-safe using streams and flatMap..
My input is a List<RequestSlot>
And the objects are nested as such:
Request
OuterItem
...
-1
votes
1
answer
94
views
Use Java 8 Optional features in the multiple null conditions
Use Java 8 Optional features in the multiple null conditions
The conditions is
Optional<Payee> payee = Optional.ofNullable(paymtTrans.getPayee());
if (payee.isPresent()
&...
-1
votes
1
answer
335
views
Syntax highlighting is not working in VS Code with the Babel extension because of optional chaining
As you can see in the picture, my syntax highlighting stops working just after I used an optional chaining in my JSX file.
Please let me know how can I fix the problem.
enter image description here
I ...
0
votes
3
answers
104
views
Is it possible to LOG different messages based on which step null is encountered while doing nested null check with Optional and map
I have a nested object which can return a null at any point of time.
Thanks to Optional and map we can now do nested calls without having to put null checks after every get.
I have a very unique ...
1
vote
1
answer
877
views
Why doesn't the optional chaining operator (?.) work with Javascript assignment? [duplicate]
When I do this:
var resetButton = document.querySelector('.resetButton');
resetButton?.onclick = function() {
//...
};
I got error: Uncaught SyntaxError: invalid assignment left-hand side.
I ...
0
votes
0
answers
38
views
Does Kotlin Have Something Similar to Swift's Guard-Let [duplicate]
In Swift I can do the following which would 1- make sure that none of these properties are nil and 2- give me access to them safely unwrapped:
// user could be nil, and lat and lon are properties on ...
0
votes
0
answers
120
views
Object is possibly undefined in Typescript, even though it's inside an if statement that checks for truthiness
I received an error in React w/ Typescript says possiblyUndefined is possibly undefined.
So I threw it in an if statement that specifics checks that it's defined before running the function inside the ...
2
votes
2
answers
1k
views
How does stacking the optional chaining (question-mark-dot) operator in Javascript behave?
I ran these experiments in the Chrome console:
a = {}
-> {}
a.n.n.n.n.n.n.n
-> Uncaught TypeError: Cannot read properties of undefined (reading 'n')
a?.n.n.n.n.n.n.n
-> Uncaught ...
2
votes
0
answers
930
views
Can't bypass optional chaining error using Babel or Webpack
I am having an issue when I am trying to use the aws-sdk v3.
I am confident it is because of the optional chaining used in this node module file.
ERROR in ./node_modules/@aws-sdk/signature-v4/dist-es/...
1
vote
1
answer
892
views
Efficient way to check if a getter will return null in Java before passing to Optional?
I have a simple data class such as
@Data
@Builder
public class MyPerson implements Serializable {
String name;
String age; //Purposely keeping as String here
String address;
}
After ...
0
votes
1
answer
301
views
Optional chaining property with 0 value
I have such code:
if (action?.notificationKey && state.notifications[action.notificationKey]) {
delete state.notifications[action.notificationKey];
}
which does not satisfy me, because ...
0
votes
0
answers
1k
views
How to resolve need additional loader installation error?
I am stuck in an issue. I can't even start making the change to the project as its not even starting. installed all the dependencies and when i started by yarn start then i got this issue.
I tried to ...
0
votes
2
answers
2k
views
Using Optionals in Java 11 to avoid null pointer exceptions using functional chaining [duplicate]
I would like to use Java 11's Optional to protect myself from the dreaded null pointer exception. I wrote a basic test to check my understanding of how they work. like. so
@Test
public void ...
10
votes
3
answers
25k
views
optional chaining in javascript causing error in eslint
i tried to use optional chaining in javascript but my eslint rules causing error .
Error : Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError ...
-1
votes
3
answers
585
views
Using JavaScript's optional chaining to conditionally render an image from an API
I'm working on building a very basic news app using the New York Times' 'top stories' API. The API is well documented and there are plenty of examples of how to get stared online. But I'm stuck with ...
1
vote
0
answers
257
views
allow optional chaining in vue by correct configuration of babel
I have vue project and i have installed few packages. When I do npm run dev it throws error for adding appropriate loader.
It seems like packages I have installed have `optional-chaining code and ...
1
vote
2
answers
1k
views
When to use true and when to use Boolean.TRUE? [duplicate]
I had written a code of which a rough snippet is as follows:
boolean isCalledFromAction = Optional.ofNullable(requestParams.get(IS_CALLED_FROM_ACTION))
.map(val -> true)...
0
votes
0
answers
1k
views
Webpack parse failed - Unexpected token on optional chaining
./src/components/BookDetails/bookdetails.jsx 147:14
Module parse failed: Unexpected token (147:14)
You may need an appropriate loader to handle this file type.
| }
| }, /*#__PURE__*/React....
-1
votes
2
answers
269
views
How do you get data showing with TypeScript Optional Chaining
I have this Try it Yourself TypeScript Optional Chaining example from W3Schools TypeScript Null & Undefined section in screenshot below.
I can see that the purpose of the example is to show you ...
1
vote
2
answers
774
views
Why a Typescript Record with a union of a primitive and a specific string makes the string mandatory?
I have this type:
type ErrorMessages = Record<number | 'default', string>;
Then, when I define a variable as const text: ErrorMessages = {403: 'forbidden'}, Typescript says that default is ...
1
vote
1
answer
374
views
Use optional property of an interface as a type ( in openapi-typescript)
How can I use a property of an interface as a type for a variable in typescript ??
Here I want to access the property: string type and use it as a type for a variable but I cannot access it.
interface ...
0
votes
0
answers
21
views
TypeScript compiler doesn't recognize check for optional object attribute in external function [duplicate]
I have a type definition with an optional member which I need to access in a function. In order to assure that the optional member is indeed set, I created a "validator"-function which ...
1
vote
0
answers
553
views
*ngIf and optional chaining
I am currently refactoring some old code and thus dealing with many redundant conditional checks. Here is a short example:
<div *ngIf="user" title="{{user?.name}}">
<...
2
votes
1
answer
222
views
Use optional chaning to increment object property
I want to increment an object value using the optional chaning operator (?.).
In my example I want to increase the age of a Person by one in a function called birthday() only if an age is set for the ...
0
votes
0
answers
395
views
null check / optional chaining for array contents? [duplicate]
we're all familiar with the
tune?.image
type of null checks in Typescript optional chaining docs
but what's the best practice for checking this with an array value?
tune.images?[0]
will not work. I ...
6
votes
3
answers
2k
views
Optional chaining and await
Is there any reason to use if to check if method exists, before calling await
if (someObject.save){
await someObject.save();
}
rather than using nullish coallescence diectly with await
await ...
0
votes
0
answers
134
views
Optional chaining like syntax but cant any resource to explain [duplicate]
I have just come across
this.person!.name
in my codebase, previously I have seen this.person?.name which is optional chaining, but I have never seen ! be used nor can I find any resource to explain it'...
-1
votes
1
answer
142
views
Can optional chaining be explicitly found in some prototype?
[Disclaimer]
I do not encourage anyone to do prototype modification in anyone's code, specially shared (or meant to play along with other's).
This is bad practice (if not plain evil).
Much like force-...
0
votes
1
answer
65
views
Is there a way to use optional chaning in qooxdoo?
I use qooxdoo 6.0 version.
Optional chaing is a very useful feature, is there a way to use it in qooxdoo?
2
votes
3
answers
1k
views
Optional chaining on function arguments
If a function f: (X => Y) | undefined is possibly undefined, and x: X is defined, then we can use optional-chaining operator ?. to apply f to x:
f?.(x) // ok, no problem if `f` is ...
0
votes
1
answer
429
views
how to use optional chaining with map operator?
I have a scenario where I am reading a yaml file and based upon the data, make calls to tag aws resources.
await Promise.all(doc?.SQS.map(async (queue)=>{
// code to tag 1 queue
}))
Now the yaml ...