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

No emit error in return value, when function type declaration not "inline" #26596

Closed
spat-ne-hochu opened this issue Aug 22, 2018 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@spat-ne-hochu
Copy link

spat-ne-hochu commented Aug 22, 2018

Environment
version
"typescript": "3.0.1",

tsconfig
json { "compilerOptions": { "target": "es6", "moduleResolution": "node", "noResolve": false, "sourceMap": true, "skipLibCheck": true, "strict": true, "noImplicitReturns": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "lib": [ "es5", "es6", "es7", "es2015", "es2015.reflect", "es2015.symbol", "es2015.promise", "es2016", "es2016.array.include", "es2017", "dom", "dom.iterable" ], "jsx" : "react", "types": [ "jasmine" ] }, "exclude": [ "node_modules" ] }

Code

type A = () => { b: number; c: number; } 
const a: A  = () => {
  return {
    b: 3,
    c: 4,
    d: 9, // no emit error, but must emit error
  };
};

const a2: () => { b: number; c: number; } = () => {
  return {
    b: 3,
    c: 4,
    d: 9, // no emit error, but must emit error
  };
};

const a3 = () : { b: number, c: number } => {
  return {
    b: 3,
    c: 4,
    d: 9, // emit error, ok!
  };
};

Expected behavior:
All three functions must emit error, because "d" field not exists in return type

Actual behavior:
Only last function emit error
Type '{ b: number; c: number; d: number; }' is not assignable to type '{ b: number; c: number; }'.
Object literal may only specify known properties, and 'd' does not exist in type '{ b: number; c: number; }'.

Playground Link: https://www.typescriptlang.org/play/#src=type%20A%20%3D%20()%20%3D%3E%20%7B%20b%3A%20number%3B%20c%3A%20number%3B%20%7D%20%0D%0Aconst%20a%3A%20A%20%20%3D%20()%20%3D%3E%20%7B%0D%0A%20%20return%20%7B%0D%0A%20%20%20%20b%3A%203%2C%0D%0A%20%20%20%20c%3A%204%2C%0D%0A%20%20%20%20d%3A%209%2C%0D%0A%20%20%7D%3B%0D%0A%7D%3B%0D%0A%0D%0Aconst%20a2%3A%20()%20%3D%3E%20%7B%20b%3A%20number%3B%20c%3A%20number%3B%20%7D%20%3D%20()%20%3D%3E%20%7B%0D%0A%20%20return%20%7B%0D%0A%20%20%20%20b%3A%203%2C%0D%0A%20%20%20%20c%3A%204%2C%0D%0A%20%20%20%20d%3A%209%2C%0D%0A%20%20%7D%3B%0D%0A%7D%3B%0D%0A%0D%0Aconst%20a3%20%3D%20()%20%3A%20%7B%20b%3A%20number%2C%20c%3A%20number%20%7D%20%3D%3E%20%7B%0D%0A%20%20return%20%7B%0D%0A%20%20%20%20b%3A%203%2C%0D%0A%20%20%20%20c%3A%204%2C%0D%0A%20%20%20%20d%3A%209%2C%0D%0A%20%20%7D%3B%0D%0A%7D%3B

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 22, 2018
@RyanCavanaugh
Copy link
Member

Duplicate of #12632

@RyanCavanaugh RyanCavanaugh marked this as a duplicate of #12632 Aug 22, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants