E
E
exelenet2020-05-03 23:09:00
typescript
exelenet, 2020-05-03 23:09:00

Push function typing. Why doesn't it come out?

Picking the typescript here means for fun:

type Shift<T extends any[]> = ((...a: T) => any) extends ((a: any, ...result: infer Result) => any) ? Result : never;
type Unshift<A, T extends any[]> = ((a: A, ...b: T) => any) extends ((...result: infer Result) => any) ? Result : never;
type First<A extends any[]> = A extends [infer F, ...any[]] ? F : never;
type Addd<A extends any[]> = ((x: any, ...args: A) => any) extends ((...args: infer U) => any) ? U : never;

type Push<
  A extends any[], 
  V,
  Acc extends any[] = [],
  Res extends any[] = [V],
  Add extends any[] = Addd<A>,
  S extends any[] = Shift<Add>,
> = {
  [K in keyof Add]:
    S['length'] extends 0 ? 
    Acc['length'] extends 0 ? Res 
    : Push<A, V, Shift<Acc>, Unshift<First<Acc>, Res>>
    : Push<Shift<S>, V, Unshift<First<S>, Acc>>
}[0];

type Arr = [1,2,3,4];
type Test = Push<Arr, 100>; // высвечивает [1,2,3,4,100]

function push<T extends any[], S>(arr: T, el: S): Push<T, S> {
  return [...arr, el]; // высвечивает что не может привести к типу... :(
}

Who knows what could be the matter?

Update
That's how it worked
function push<T extends any[], S>(arr: T, el: S): Push<T, S> {
  const newArr = [...arr];
  newArr.push(el);
  return newArr as Push<T, S>;
}

But with such success, I can stupidly return the original array and the compiler will not swear! There is still a suspicion that the type turned out to be heavy ... In general, sadness :'(

Answer the question

In order to leave comments, you need to log in

14 answer(s)
D
Dmitry Belyaev, 2020-05-04
@bingo347

TS does not know how to work with a spread operator that is not at the end of the array, so far you can only put up with it.
Well, you should be more careful with recursive types, they may well hang the typechecker, since TS typing is Turing complete, or rather, it is a lambda of calculus in its purest form, which, however, from the point of view of computability, is one and the same.

E
exelenet, 2020-05-17
@exelenet

In general, for those who come here with the same question or a similar one, I will answer that the typescript is not sure that the recursion will be final, since the any[] array can be of any size. Therefore, we need to implement something like the top bar of the call stack - in a simple way, we just look at the size of the resulting value and if it is no longer in our range, then return the never type and exit the recursion abnormally)

M
Maxim, 2013-07-12
@Pasha4ur

You need to read about the classpath, for example, here for a start - www.skipy.ru/technics/likbez.html

O
Oleg Dyachenko, 2014-05-27
@OLDJman

Good afternoon. I registered specifically to describe the solution to this problem.
I myself suffered two weeks ago and thought that I was mdk, because nothing works.
This is not entirely true, so read below.
The essence of the problem:
When repeating the video lesson, or rather the actions in it, the desired result was not displayed. More specifically, the Hellow World message output program was written in the WIndows console
Namely, the compiled java file was called by the java NAME.class command, and the corresponding error came out: "could not find or load main class class_NAME"
t
How I decided:
You need to call the already COMPILED file with the command [b][i] java NAME [/i][/b]And that's it!!! Then the console normally plays the program, if, of course, there are no errors in it.
Perhaps for a programming guru, this is an obvious answer, and it seems that this is the norm, but as a 3rd week student of Java, on the second day I really did not understand what the essence of the problem was. And since, despite the fact that the training is difficult, I was offended and did not understand why it did not work.
Below I will write a complete algorithm for working with a Java machine at a basic level, maybe it will help someone.
1) Download the JDK from the site (I won’t give a link, you can definitely find it)
2) Install the downloaded package
3) Connect the installed Java machine to our system (I have Windows 7)
a) go to the folder with the installed package and find the file java.exe (on my Windows it was: C:\Program Files\Java\jdk1.8.0_05\bin\java.exe)
_______I draw your attention to the fact that it is not necessary to find the java.exe file, I just did this =)
b) Right-click on java.exe, select "Properties". In the pop-up window, in the "General" tab, look for the line "Location". Highlight and copy the path (I have it C:\Program Files\Java\jdk1.8.0_05\bin)
c) Open start, hover the mouse over "My Computer", press the right mouse button, click "Properties".
d) In the window that opens, on the left, click the "Advanced system settings" button
e) In the "System Properties" window that opens, in the "Advanced" tab (it is selected / open by default), at the bottom we look for the "Environment Variables" button. Click on it
e) In the "Environment Variables" window that opens,
g) In the window that opens, enter the name of the variable, I have it Path (I don’t know if another is possible), and in the “Value” field enter our copied path (C:\Program Files\Java\jdk1.8.0_05\bin)
e ) Click "OK", save everything.
4) Check our java machine
a) Launch the console (either WIN + r => then cmd and Enter or Start => type cmd in the search above the start and press Enter)
b) In the console that opens, enter java
c) After that, a bunch of java commands, service information, etc., if everything is connected normally, if not, it will give an error like "There is no such command", or something similar. If something is wrong, do the steps above again.
5) If everything is ok, go ahead. We create our simple java program.
6) Open notepad and write the code below:
[code=java]public class Hellow
{
public static void main(String[] args)
{
System.out.println("Hellow World!");
}
}
[/code]
I draw your attention to the fact that there are frequent mistakes here, the "main" method is not written, and "println" is erroneously written. Check carefully what you typed. If you are an absolute beginner, then before you understand what is happening, it will take time, so do not be smart, and check up to every comma.
7) Save our file. Here, pay attention to the detail: "In the line public class Hellow, the word Hellow is the name of the class, in fact, it can be almost anything, but it is very important that this name matches the name of your file that you save, and if you it is capitalized, so the file name must be capitalized.Today I found out that in java it is generally customary to give class names with a capital letter, so do not be lazy, write with a capital letter, but with a small one, it will also not be a mistake, the main thing is to save rule, what is the name of the class, so is the file
name.Save with the extension .java
As a result, in our example, you need to save the file in this form Hellow.java (there seem to be exceptions, you can call the file whatever you want, but I’m not sure, alas, but I don’t know this yet, so do as it is written above, then if find out the truth, write to me, I will be grateful)
8) At this stage, we just created a file that the program can compile, the second mistake is to want to immediately open this file and execute it. I don’t know the details, but the bottom line is that modern operating systems and processors don’t know how to do this, I don’t know why, it seems that there is something with the computer production process, they seem to be 4-bit, or vice versa, not 4-bit, in short, this code is just you can’t run it like that, you need to compile it, translate it into a machine language that can be run, for this we go to the next step
9) We start the console , in it we go to the folder with our Hellow.java
file you need to enter the command cd directory_name. For example, we need to go to the Desktop directory / folder, enter cd desktop. Of course, we can go there only if this folder is in the place where we are. The current location is displayed to the left of the command being entered in the form C:\Program Files\
To return to the root folder of drive C, you need to type cd \
Somehow you can climb the directory above, and other actions, but here already google, I have a different task.
The dir command shows a list of all folders in a given directory/folder
If you know the exact path to the directory, you can write the full path from anywhere in the form C:\Program Files\Java\... and you will get there. Be sure to put \ at the end, and at the beginning drive C for example. Otherwise, you won't be able to enter.
The help command displays a sort of basic list of possible commands
Total, the algorithm of actions to get into our folder where the Hellow.java file is located,
enter cd \
then dir
read what is there, go to the desired folder with the cd command ...
again dir
and so on
Of course, you need understand for yourself where your file is located, for example, the path for a file saved on the desktop will be c:\users\USERNAME\desktop\
10) Now we need to compile the Hellow.java file. To do this, write javac Hellow.java in the console
If everything is OK, after a couple of seconds the command input field will light up again.
If an error pops up, as a rule it is some kind of jamb at the compilation stage, read what is written there, they always write what the error was. Most often this is a jamb in the code. Go into your file and check the lines carefully. The more often you read mistakes, the faster you will learn to recognize them. Knowing English will make your work 100,500 times easier.
11) When the file is compiled, it is saved in the same folder as the main one (Hellow.java), taking a name, like a class, these are details, alas, I don’t know them, but the extension will become .class In our case, it will be file Hellow.class
12) Only now we can run this file, to check that we have it in the folder where we are, enter the dir command in the console
we see our file, and enter the command java Hellow
And I pay attention, not java Hellow.class , namely java Hellow. Since the first option will give an error, why, I'm sorry, I don't know.
13) in the console, on a new line, the message Hello World!
All.
Here is the actual algorithm for launching Java programs from the Windows console.
I do not pretend to be complete, I removed cool and new material, I just suffered with this problem for two weeks myself, and wrote code in the IDEA developer environment. And he was tormented by the fact that, having already some idea of ​​\u200b\u200bJava, he could not run it through the console. It tormented me, so when I watched the next lecture, I saw what my mistake was, it dawned on me, and I solved my problems by removing the stone from my soul, and as a result, I calmed down. To celebrate, I decided that if there is at least one other person in the world who this information can help, then so be it.
Thank you for your attention and sorry in advance for any mistakes. If someday it will be necessary, maybe I will correct all the shortcomings of my short article, but for now, keep it as it is.
Good luck with programming!

S
SSiarhei, 2013-07-13
@SSiarhei

Of course, netbins is good and clearly worth setting up, but you also need to understand how everything starts.
1. Remove the classpaf from the environment variables altogether. He is absolutely useless there.
2. PATH=D:\Program Files\Java\jdk1.7.0_25\BIN, the rest is superfluous.
after that it should be launched from any folder. Does not help - unsubscribe, we'll figure it out
ps you're not running correctly at all. you must be in the folder where you have the class, and not just in c:\

A
Andrew, 2013-07-12
@xaoc80

I advise you to install NetBeans, then there will be no headache about launching programs directly from the ID

S
Sergey Galkin, 2013-07-13
@Larrikin

Tried to create .bat files? It's just a text file one line one command.
start.bat can be created anywhere

@echo off
d:
cd "\Program Files\Java\jdk1.7.0_25\BIN"
javac HelloWorld.java

I didn’t check for full functionality, it’s more like just a hint
The first line removes the output of the command text
The second line sets the D drive as the current:
The third goes to the folder you need (which one you must determine)
The fourth starts

A
AlexanderNeznakhin, 2016-12-26
@AlexanderNeznakhin

I have a slightly different experience.
When an error "Error: Could not find or load..." ran the file given the package name.
Let me explain.
The Hellow.java file looked like this:
package hellow;
public class Hellow {
public static void main(String[] args) {
System.out.println("Hellow, World!");
}
}
The compiled Hellow.class file is created in the hellow directory, according to the package name.
Launched the file from the package level (i.e. from the directory where the hellow directory containing the Hellow.class file is "visible", but the Hellow.class file itself is "not visible").
At the same time, the command specified the package name:
java hellow.Hellow
Received the expected output:
Hello, World!

T
Tupica, 2017-09-26
@Tupica

I could not run my Java code, I suffered, I tried all sorts of different things.
That's all that is written did not help at all.
The problem turned out to be that I stupidly created a Java project whose code began with "package Name;"
As soon as I removed the line with package in my codes, everything immediately started to run in this way with java ProgramName without any problems

D
Dom Topor, 2013-11-14
@deleted-Ari100teLL

First you need to install the JVM (Java Virtual Machine), and then set the environment variables:
http://java-course.ru/begin/install-jdk/
And try to download and install the IDE:
       1)Intellij Idea
       2)NetBeans
       3)Eclipse

M
Mr Jenkins, 2013-12-01
@qassarb

I give an example on Linux, because. I consider it more obvious.
The same is true for wine carriers.
So:

$ pwd
/home/user
$ mkdir test
$ vi ./test/Main.java.

Sample contents of Main.java:
package test;
class Main {
     public static void main (String args[]) {
          System.out.println ("Hi, tushkan!")
     }
}

to compile you need:
$ cd /home/user
$ javac test/Main.java
$ java test.Main

In general, you can read it here: docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
BR < qassarb

A
AndShoot, 2018-03-30
@AndShoot

aboutprognotmuch.blogspot.ru/2018/03/helloworld-ja...
Here it is short and clear.

P
Pavel Rozhkov, 2020-10-04
@Pavel_3659

Running Example.class turned out like this for me.
1) make code page 1251 so that Russian letters
Chcp 1251 are displayed correctly
2) make our jdk a separate disk
subst j: "c:\Program Files x86\Java\jdk1.7"
3) In the command line, execute the command (java.exe from the folder bin, mine is J:\bin)
java.exe -classpath "X:\Learn\out\production\Learn" Example

S
Sergey_USB, 2020-10-14
@Sergey_USB

I had such crap while the folder with the project was called in Russian, renamed the project folder - and everything is OK

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question