Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ~ (tilde) doing in this javascript import?

In the App.tsx taken from the fountain-webapp typescript MVC sample (http://fountainjs.io/), the import contains the following line:

import {IDispatch} from '~react-redux~redux'; 

Visual Studio 2017 underlines this line ("cannot find module") however it does work in the browser. I've never seen this syntax before and don't know what it's attempting to do?

There's an open ticket mentioning it here: https://github.com/FountainJS/generator-fountain-react/issues/70

like image 674
Rich Avatar asked Mar 30 '17 09:03

Rich


People also ask

What does tilde do in JavaScript?

The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000).

What is in import in JavaScript?

Introduction to the JavaScript import() The import() allows you to dynamically import a module when needed. Here is how the import() works: The import() accepts a module specifier ( moduleSpecifier ) that has the same format as the module specifier used for the import statement.

What is double tilde in JavaScript?

The “double tilde” (~~) operator is a double NOT Bitwise operator. Use it as a substitute for Math. floor(), since it's faster.


1 Answers

Tilde (~) used in conjunction with webpack means that a lookup is performed against node_modules to resolve the path.

In other words, it is a predefined alias that resolves to node_modules.

import { IDispatch } from '~react-redux~redux'; 

is equivalent to

import { IDispatch } from 'relative_path_to_node_modules/react-redux~redux'; 

EDIT: Unfortunately, I can't cite any documentation about this, it is based on experience, you're welcome to edit this post with a more accurate description.

Now I've noticed the ~redux part as well, so you might want to check out the other answer, because I'm puzzled there as well.

like image 67
Lyubomir Avatar answered Oct 13 '22 02:10

Lyubomir