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

Typescript can't compile success while using some Symbol attributes #20572

Closed
MSDimos opened this issue Dec 8, 2017 · 4 comments
Closed

Typescript can't compile success while using some Symbol attributes #20572

MSDimos opened this issue Dec 8, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@MSDimos
Copy link

MSDimos commented Dec 8, 2017

TypeScript Version: 2.6.2

Code

class Ite {

    [Symbol.toPrimitive](hint): number | string {
        switch (hint) {
            case 'number':
                return 100;
            case 'string':
                return "100";
            case "default":
                return "default";
            default:
                return null;
        }
    }
}
let ite: Ite = new Ite();

/*The following code hints the errors */
console.log(ite == 100);
console.log(ite + 100);
console.log(ite * 2);

Expected behavior:
JavaScript can run normally, and display the following content at the console

false
default100
200

but typescript can't compile success. And many of the rest of the Symbol attributes are not normally identified. Such as Symbol.species.

@kitsonk
Copy link
Contributor

kitsonk commented Dec 8, 2017

As the form noted, you need to provide a fully reproducible example.

You need to provide more basic information. What is your tsconfig.json or what arguments are you passing in tsc. When you say can't compile success what errors are you getting?

@MSDimos
Copy link
Author

MSDimos commented Dec 9, 2017

@kitsonk Thanks for your reply
This is my tsconfig.json

{
    "compilerOptions": {
        "target": "es2015",
        "module": "amd",
        "jsx": "react",
        "outDir": "./dist",
        "moduleResolution": "node"
    },
    "compileOnSave": true,
    "include": [
        "./*"
    ]

}

This is my package.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "typescript": "^2.6.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

I added a command script in package.json, and using npm run build to run tsc index.ts.
and this is what error says

index.ts(3,6): error TS2304: Cannot find name 'Symbol'.
index.ts(19,13): error TS2365: Operator '==' cannot be applied to types 'Ite' and 'number'.
index.ts(20,13): error TS2365: Operator '+' cannot be applied to types 'Ite' and '100'.
index.ts(21,13): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

But the code is in accordance with the ES6 specification, and it is reasonable that it shouldn't be wrong.
Thank

@mhegazy
Copy link
Contributor

mhegazy commented Jan 10, 2018

Duplicate of #4538

@mhegazy mhegazy marked this as a duplicate of #4538 Jan 10, 2018
@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 10, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants