Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional Instantiation #57713

Closed
sanskar-19 opened this issue Mar 9, 2024 · 5 comments
Closed

Functional Instantiation #57713

sanskar-19 opened this issue Mar 9, 2024 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@sanskar-19
Copy link

sanskar-19 commented Mar 9, 2024

πŸ”Ž Search Terms

functional instantiation, es5

πŸ•— Version & Regression Information

  • This is a bug

⏯ Playground Link

No response

πŸ’» Code

interface UserProfile {
    fName: string;
    lName: string;
    printFirstName: () => void;
    printLastName: () => void;
}

// Define user class
function User(this: UserProfile, fName: string, lName: string) {
    if (!(this instanceof User)) {
        return (new User(fName, lName));
    }

    this.fName = fName;
    this.lName = lName;
}

// Define prototype for print First name
User.prototype.printFirstName = function () {
    console.log(this.fName);
}

// Define prototype for print  Last name
User.prototype.printLastName = function () {
    console.log(this.lName);
}

const user = new User('John', 'Doe');

πŸ™ Actual behavior

new expression whose target lacks a constructor signature. Switching back to normal JS works as expected.

πŸ™‚ Expected behavior

Should compile without issues. It should point to the constructor by default present in the prototype object.

Additional information about the issue

Typescript config :

{
  "compilerOptions": {
    "target": "es2016",                             
    "module": "ESNext",                             
    "esModuleInterop": true,                        
    "forceConsistentCasingInFileNames": true,       
    "strict": true,                                 
    "outDir": "build",
    "skipLibCheck": true
  }
}

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 9, 2024

This is working as intended. See #13013 (comment).

You can add an explicit this parameter.

@sanskar-19
Copy link
Author

sanskar-19 commented Mar 9, 2024

This is working as intended. See #13013 (comment).

You can add an explicit this parameter.

It actually is not :
new expression whose target lacks a constructor signature.

image

Can you please explain this !?

As far as I know, the constructor should have been by default referred to the function prototype constructor property.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 11, 2024
@RyanCavanaugh
Copy link
Member

TypeScript only supports classes written with the class keyword.

@snarbies
Copy link

I imagine an interface with a construct signature could do the trick if we had Describe function signature using an interface #34319

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants