I realize this is really simple but typescript seems to have changed a lot in the last years and i just cant get this done with previous answers i found here on stack overflow.
let myfunction = something that returns a function
export myfunction;
I get an error «declaration or statement expected»
How can i export a function from a really simple ts file to be able to use the function in another ts file?
asked Feb 2, 2017 at 2:52
Joaquin BrandanJoaquin Brandan
2,6073 gold badges18 silver badges21 bronze badges
It seems that
let myfunction = something that returns a function
export {myfunction};
will do the trick.
Tagc
8,5687 gold badges58 silver badges109 bronze badges
answered Feb 2, 2017 at 3:00
Joaquin BrandanJoaquin Brandan
2,6073 gold badges18 silver badges21 bronze badges
0
Use
export default myfunction
if you only have this function to export from this file. Otherwise use
export { myfunction, <other exports> }
to export myfunction
along with other types to export
answered Jun 5, 2018 at 10:31
Philip BijkerPhilip Bijker
4,8652 gold badges40 silver badges44 bronze badges
You can call a function
or instantiate a class
from another file using modular top-level import
and export
declarations.
file1.ts
// This file is an external module because it contains a top-level 'export'
export function foo() {
console.log('hello');
}
export class bar { }
file2.ts
// This file is also an external module because it has an 'import' declaration
import f1 = module('file1');
f1.foo();
var b = new f1.bar();
answered Feb 2, 2017 at 2:56
1
I realize this is really simple but typescript seems to have changed a lot in the last years and i just cant get this done with previous answers i found here on stack overflow.
let myfunction = something that returns a function
export myfunction;
I get an error «declaration or statement expected»
How can i export a function from a really simple ts file to be able to use the function in another ts file?
asked Feb 2, 2017 at 2:52
Joaquin BrandanJoaquin Brandan
2,6073 gold badges18 silver badges21 bronze badges
It seems that
let myfunction = something that returns a function
export {myfunction};
will do the trick.
Tagc
8,5687 gold badges58 silver badges109 bronze badges
answered Feb 2, 2017 at 3:00
Joaquin BrandanJoaquin Brandan
2,6073 gold badges18 silver badges21 bronze badges
0
Use
export default myfunction
if you only have this function to export from this file. Otherwise use
export { myfunction, <other exports> }
to export myfunction
along with other types to export
answered Jun 5, 2018 at 10:31
Philip BijkerPhilip Bijker
4,8652 gold badges40 silver badges44 bronze badges
You can call a function
or instantiate a class
from another file using modular top-level import
and export
declarations.
file1.ts
// This file is an external module because it contains a top-level 'export'
export function foo() {
console.log('hello');
}
export class bar { }
file2.ts
// This file is also an external module because it has an 'import' declaration
import f1 = module('file1');
f1.foo();
var b = new f1.bar();
answered Feb 2, 2017 at 2:56
1
- HowTo
- TypeScript Howtos
- Declaration or Statement Expected Error …

This tutorial explains the Declaration or statement expected
error in JavaScript or TypeScript and why the compiler throws this error. All the major reasons for this error will be discussed, and how it can be avoided among the developer’s community.
Declaration or statement expected
Error in JavaScript or TypeScript
The Declaration or statement expected
error in JavaScript or TypeScript occurs when we have a syntax error in the code.
For instance, consider the destructuring of an object in the file with the wrong syntax, exporting a module in the file with the wrong name, or having a missing or inconsistent bracket.
Consider the following example of a code where a different Declaration or statement expected
occurs due to the syntax error inside the code.
let oneData: number;
const obj = {
val: 1,
};
// 1. ⛔️ Parsing error: Declaration or statement expected.
{ oneData } = obj; // 👈️ this must be wrapped in parenthesis
const sumObj = (a: number, b: number) => a + b;
// 2. ⛔️ Error: Parsing error: Declaration or statement expected.eslint
export sumObj // 👈️ should be export {sum}
// 3. Make sure you're not using reserved words
const caseVal = 'hello world' // 👈️ case is reserved word
The above code produces the following errors, which are written below.
//output or errors
Variable 'one' is used before being assigned.
Declaration or statement expected. This '=' follows a block of statements, so if you intend to write a destructuring assignment, you might need to wrap the whole assignment in parentheses.
Declaration or statement expected.
'case' is not allowed as a variable declaration name.
Variable declaration expected.
Variable declaration expected.
Consider the following code, compiled correctly with no Declaration or statement expected
errors.
let val: number;
const obj = {
val: 1,
};
// ✅ OK
({ val } = obj); // 👈️ this must be wrapped in parenthesis
console.log(val); // 👉️ 1
The above code produces the following output.
Declaration or statement expected
error also arises sometimes when exporting something that has been declared previously. Whenever this needed to be done, wrap the export in curly braces.
const objSum = (a: number, b: number) => a + b;
// ✅ OK
export { objSum };
Ibrahim is a Full Stack developer working as a Software Engineer in a reputable international organization. He has work experience in technologies stack like MERN and Spring Boot. He is an enthusiastic JavaScript lover who loves to provide and share research-based solutions to problems. He loves problem-solving and loves to write solutions of those problems with implemented solutions.
Содержание
- Parsing error declaration or statement expected
- Declaration or statement expected error in TypeScript #
- Declaration or Statement Expected Error in TypeScript
- Declaration or statement expected Error in JavaScript or TypeScript
- Thread: error:expected declaration or statement at end of input
- linked list and read strings-problem
- c++ expected a declaration [ SOLVED]
- Solution-1 | Expected a declaration – When used namespace instead of class.
- Solution-2 | Expected a declaration c++ – When statement is not inside function and added out of scope
- Solution-3 | c++ Expected a declaration – When return statement is out side main
Parsing error declaration or statement expected
Reading time В· 2 min
Declaration or statement expected error in TypeScript #
The «Declaration or statement expected» error occurs when we have a syntax error in our code, e.g. when destructuring, exporting, or have a missing or inconsistent bracket. To solve the error, make sure to correct any syntax errors in your code.
Here are 3 examples of how the error occurs.
The first example shows how the error occurs when destructuring. To be able to destructure and reassign an already-declared variable, wrap the statement in parentheses.
You might also get the error when exporting something that you previously declared. When you declare something and export it on another line, wrap the export in curly braces.
We are basically exporting an object that contains one or more named exports.
You don’t have to use curly braces if you export and declare the variable in a single statement.
Another common cause of the error is having a missing bracket somewhere in your code.
Carefully check your code for a missing curly brace, parenthesis, square bracket, etc.
If you use IntelliJ as your IDE and are getting the «Declaration or statement expected» error, try closing and re-opening the file or restarting your IDE as it sometimes glitches.
Make sure you aren’t using any reserved words when declaring variables.
Words like case , class , Error , etc are reserved, so we’re not allowed to use them as variable names.
Источник
Declaration or Statement Expected Error in TypeScript
This tutorial explains the Declaration or statement expected error in JavaScript or TypeScript and why the compiler throws this error. All the major reasons for this error will be discussed, and how it can be avoided among the developer’s community.
Declaration or statement expected Error in JavaScript or TypeScript
The Declaration or statement expected error in JavaScript or TypeScript occurs when we have a syntax error in the code.
For instance, consider the destructuring of an object in the file with the wrong syntax, exporting a module in the file with the wrong name, or having a missing or inconsistent bracket.
Consider the following example of a code where a different Declaration or statement expected occurs due to the syntax error inside the code.
The above code produces the following errors, which are written below.
Consider the following code, compiled correctly with no Declaration or statement expected errors.
The above code produces the following output.
Declaration or statement expected error also arises sometimes when exporting something that has been declared previously. Whenever this needed to be done, wrap the export in curly braces.
Ibrahim is a Full Stack developer working as a Software Engineer in a reputable international organization. He has work experience in technologies stack like MERN and Spring Boot. He is an enthusiastic JavaScript lover who loves to provide and share research-based solutions to problems. He loves problem-solving and loves to write solutions of those problems with implemented solutions.
Источник
Thread: error:expected declaration or statement at end of input
Thread Tools
Search Thread
Display
linked list and read strings-problem
GO TO POST 6 (is my new problem)
<
this solved
I am getting this message when i am trying to compile the code
series1.c:44: σφάλμα: expected declaration or statement at end of input
series1.c:44: σφάλμα: expected declaration or statement at end of input
where is the mistake?(maybe any missing < or >)
Last edited by alzar; 09-20-2007 at 12:23 PM .
If you indent your code correctly, you’ll notice that you are missing two curly braces.
Count the number of closing braces which your main() seems to have.
Read the FAQ on why you shouldn’t use gets()
I am getting this message when i am trying to compile the code
series1.c:44: σφάλμα: expected declaration or statement at end of input
series1.c:44: σφάλμα: expected declaration or statement at end of input
where is the mistake?(maybe any missing < or >)
This code was a part of an exercise that i have, so i forget to close switch and main.Sorry
This is an infinite loop if k is anything other than 0. There is no modification of k beyond the initial scanf call.
= is for assignment, == is to test for equality. Second, head is not in scope here. Third, currenta hasn’t yet been initialized the first time the function is called, head is equal to NULL and in fact does not change it’s value so it will always be NULL and the rest of the code (the else part) will never get executed.
newa never gets initialized to anything valid (in the above if test it is effectively set to the same random address as currenta). Same thing with firsta and since currenta points to this random memory address I certainly wouldn’t want to try and access its next pointer member. I think newa is really supposed to be newnode here in this function and firsta is maybe supposed to be set to head which gets passed in as a parameter?
The whole function should either be returning a pointer to a new node, or setting an argument passed into the function to the newly allocated memory, or something like that. Bottom line is there’s a lot wrong with this code that needs fixing.
Источник
c++ expected a declaration [ SOLVED]
When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code.
Main reasons for errors are:
- Incorrect use/declaration inside namespace
- Statements are added out of scope
- Required statement need to add inside main/function
Solution-1 | Expected a declaration – When used namespace instead of class.
When developer is mostly worked in java/C# then possibly then make mistake in c++ code for namespace. If we check here given syntax is not correct for name space. For c++ we use public: for any functions.
c++ expected a declaration error will come here. Here if we want to resolve error then instead of namespace we can use class. Inside Class A we can add public access specifier using colon(:) then we have to write methods. Here public static void print() is not valid in c++ so it will give error for your c++ code.
Solution-2 | Expected a declaration c++ – When statement is not inside function and added out of scope
Here at place of if condition it is giving us error that declaration is expected. If you see give code then you will realize that condition is not in any scope and if require inside some function.
Here we have used if statement out side main() and also it is not inside any function. So it is out of scope. We will either require to add given code inside any of function or we can add inside main function then only expected a declaration error will be resolved.
We are successfully able to resolve this error by adding our if condition logic inside void func() , now you can use this function call to execute your logic.
Solution-3 | c++ Expected a declaration – When return statement is out side main
Here we have added return 0 outside main function. So it is giving us error for expected a declaration. Because return statement is require inside of any function. We can not write direct statement which is out of scope.
For solving this issue you can add return inside your main function. If similar errors you are getting for other class/function then you can first check given statement is inside scope or it require to add in any function.
CONCLUSION:
Whenever you get error for c++ expected a declaration then you can check that your error indicated code is inside function scope ? or you have used namespace improperly. Mostly by checking it you will find reason for error and can resolve easily by adding code inside scope.
Источник
Всем привет.Стараюсь собрать проект с помощью babel,webpack.Проект на typescript,react,scss.Получаю ошибку:
ERROR in src/components/App/app.scss:1:0
[unknown]: Parsing error: Declaration or statement expected.
> 1 | .title{
2 | color:red;
3 | text-align: center;
4 | }
ERROR in src/components/App/app.scss.d.ts:3:11
@typescript-eslint/no-empty-interface: An empty interface is equivalent to `{}`.
1 | // This file is automatically generated.
2 | // Please do not change this file!
> 3 | interface CssExports {
| ^^^^^^^^^^
4 |
5 | }
6 | export const cssExports: CssExports;
webpack 5.11.1 compiled with 3 errors in 3556 ms
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! chat@1.0.0 build-dev: `webpack --env.mode=development`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the chat@1.0.0 build-dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/nikita/.npm/_logs/2020-12-31T16_13_00_338Z-debug.log
.babelrc
{
"presets": [
[
"@babel/env",
{
"corejs": 3,
"useBuiltIns": "usage",
"debug": true,
"modules": false
}
],
"@babel/react",
"@babel/typescript"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
webpack.config.js
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = (env = {}) => {
const { mode = 'development' } = env;
const isProd = mode === 'production';
const isDev = mode === 'development';
const getStyleLoaders = () => {
return [
isProd ? MiniCssExtractPlugin.loader : 'style-loader'
]
}
const getPlugins = () => {
const plugins = [
new HtmlWebpackPlugin({
title: 'Chat',
buildTime: new Date().toISOString(),
template: 'public/index.html'
}),
new ForkTsCheckerWebpackPlugin({
async:false,
eslint:{
files: "./src/**/*"
}
})
];
if (isProd) {
plugins.push(new MiniCssExtractPlugin({
filename: 'main-[hash:8].css'
}))
}
return plugins
}
return {
mode: isProd ? 'production' : isDev && 'development',
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output:{
filename: isProd ? 'main-[hash:8].js' : undefined
},
module: {
rules: [
{
test:/.(ts|js)x?/,
exclude:/node_modules/,
use:{
loader:"babel-loader",
options:{
presets:[
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
}
},
// Loading images
{
test: /.(png|jpg|gif|ico|jpeg)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
name: '[name]-[sha1:hash:7].[ext]'
}
}
]
},
// Loading fonts
{
test: /.(ttf|otf|eot|woff|woff2)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
name: '[name]-[sha1:hash:7].[ext]'
}
}
]
},
{
test: /.(sa|sc|c)ss$/,
use: [
...getStyleLoaders(),
"css-modules-typescript-loader",
'css-loader',
"postcss-loader",
"sass-loader"
]
}
]
},
plugins: getPlugins(),
devServer: {
open: true,
}
}
}
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowJs": true,
"jsx": "react",
"outDir": "./dist",
"rootDir": "./",
"isolatedModules": true,
"strict": true,
"moduleResolution": "node",
"baseUrl": "./src",
"typeRoots": ["node_modules/@types", "src/typings"],
"esModuleInterop": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
eslintrc.json
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"react-hooks"
],
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"react/prop-types": "off"
},
"settings": {
"react": {
"pragma": "React",
"version": "detect"
}
}
}