簡體   English   中英

打字稿:對象文字只能指定已知屬性

[英]Typescript: Object literal may only specify known properties

我有一個代碼:

// Library interfaces
interface PersonalData {
    name: string;
    age: number;
    nick: string;
    friends?: Friends[];
}

interface Friends extends PersonalData { }

// I want to add new props girls in friend interface
interface NewPropGirl {
    girl: string;
    friends?: NewPops[];
}

interface NewPops extends NewPropGirl { }

const test = (): Friends[] & NewPops[]  => {
    return [{
        name: 'Oleg',
        age: 25,
        nick: 'yyy',
        girl: 'Test',
        friends: [{
            name: 'Roman',
            age: 22,
            nick: 'bob',
            girl: 'Test',
        }]
    }];
}

我想在庫界面中添加一個新的界面參數girl 在這段代碼中,我在第 31 行有一個錯誤

打字稿版本 3.5.3

Type '{ name: string; age: number; nick: string; girl: string; friends: { girl: string; }[]; }[]' is not assignable to type 'Friends[] & NewPops[]'.
  Type '{ name: string; age: number; nick: string; girl: string; friends: { girl: string; }[]; }[]' is not assignable to type 'Friends[]'.
    Type '{ name: string; age: number; nick: string; girl: string; friends: { girl: string; }[]; }' is not assignable to type 'Friends'.
      Object literal may only specify known properties, and 'girl' does not exist in type 'Friends'.

游樂場鏈接: 游樂場

我已將 PersonalData 擴展到 NewPropGirl 以讓您的結果包含一個額外的字段並將其作為返回類型

interface PersonalData {
  name: string;
  age: number;
  nick: string;
  friends ? : Friends[];
}

interface Friends extends PersonalData {}

// I want to add new props
interface NewPropGirl extends PersonalData {
  girl: string;
  friends ? : NewPops[];
}

interface NewPops extends NewPropGirl {}

const test = (): NewPops[] => {
  return [{
    name: 'Oleg',
    age: 25,
    nick: 'yyy',
    girl: 'Test',
    friends: [{
      name: 'Roman',
      age: 22,
      nick: 'bob',
      girl: 'Test',
    }]
  }];
}
interface PersonalData {
    name: string;
    age: number;
    nick: string;
    friends?: Friends[];
}

interface Friends extends PersonalData { }

// I want to add new props
interface NewPropGirl extends PersonalData {
    girl: string;
    friends?: NewPops[];
}

interface NewPops extends NewPropGirl { }

const test = (): Friends[] | NewPops[]  => {
    return [{
        name: 'Oleg',
        age: 25,
        nick: 'yyy',
        girl: 'Test',
        friends: [{
            name: 'Roman',
            age: 22,
            nick: 'bob',
            girl:"Bella"
        }]
    }];
}

看看它是否符合您的需求。 操場

Typescript 錯誤:Object 字面量只能指定已知屬性,並且“組件選項”類型中不存在“路由器” <vue< div><div id="text_translate"><p> 我剛剛將 typescript 添加到我現有的 vuejs 應用程序中,我得到了一個Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt;'錯誤。 我的理解是我不需要安裝@types/vue 或@types/vue-router 來解決這個問題。 有人可以建議新手嗎?</p><p> <strong>~\src\router.js</strong></p><pre> import Vue from 'vue'; import Router from 'vue-router'; Vue.use(Router); const router = new Router({............ }); export default router;</pre><p> <strong>堆棧跟蹤:</strong></p><pre> ERROR in ~/src/main.ts(126,10): 126:10 No overload matches this call. Overload 1 of 3, '(options?: ThisTypedComponentOptionsWithArrayProps&lt;Vue, object, object, object, never&gt; | undefined): CombinedVueInstance&lt;Vue, object, object, object, Record&lt;...&gt;&gt;', gave the following error. Argument of type '{ router: any; store: any; i18n: any; acl: any; render: (h: CreateElement) =&gt; VNode; }' is not assignable to parameter of type 'ThisTypedComponentOptionsWithArrayProps&lt;Vue, object, object, object, never&gt;'. Object literal may only specify known properties, and 'router' does not exist in type 'ThisTypedComponentOptionsWithArrayProps&lt;Vue, object, object, object, never&gt;'. Overload 2 of 3, '(options?: ThisTypedComponentOptionsWithRecordProps&lt;Vue, object, object, object, object&gt; | undefined): CombinedVueInstance&lt;Vue, object, object, object, Record&lt;...&gt;&gt;', gave the following error. Argument of type '{ router: any; store: any; i18n: any; acl: any; render: (h: CreateElement) =&gt; VNode; }' is not assignable to parameter of type 'ThisTypedComponentOptionsWithRecordProps&lt;Vue, object, object, object, object&gt;'. Object literal may only specify known properties, and 'router' does not exist in type 'ThisTypedComponentOptionsWithRecordProps&lt;Vue, object, object, object, object&gt;'. Overload 3 of 3, '(options?: ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt; | undefined): CombinedVueInstance&lt;...&gt;', gave the following error. Argument of type '{ router: any; store: any; i18n: any; acl: any; render: (h: CreateElement) =&gt; VNode; }' is not assignable to parameter of type 'ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt;'. Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt;'. 124 | 125 | Vue.config.productionTip = false; &gt; 126 | new Vue({router, store, i18n, acl, render: h =&gt; h(App)}).$mount('#app'); | ^ Version: typescript 3.8.3</pre><p> <strong>package.json:</strong></p><pre> { "name": "video-audio-transcription", "version": "4.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "test:unit": "vue-cli-service test:unit", "lint": "vue-cli-service lint" }, "dependencies": { "@aws-amplify/ui-vue": "^0.2.3", "@chenfengyuan/vue-countdown": "^1.1.2", "@react-native-community/netinfo": "^5.5.0", "@syncfusion/ej2-vue-grids": "^18.1.46", "@types/chart.js": "^2.9.20", "@types/vue2-hammer": "^2.1.0", "ag-grid-community": "^21.0.1", "ag-grid-vue": "^21.0.1", "algoliasearch": "^3.33.0", "apexcharts": "^3.6.12", "aws-amplify": "^3.0.9", "axios": "^0.19.0", "chart.js": "^2.8.0", "core-js": "2.6.5", "echarts": "^4.2.1", "file-saver": "2.0.1", "firebase": "^6.0.4", "instantsearch.css": "^7.3.1", "jsonwebtoken": "^8.5.1", "material-icons": "^0.3.1", "perfect-scrollbar": "^1.4.0", "postcss-rtl": "^1.5.0", "prismjs": "^1.16.0", "vee-validate": "^2.2.8", "vue": "^2.6.11", "vue-acl": "4.0.7", "vue-apexcharts": "^1.3.5", "vue-awesome-swiper": "^3.1.3", "vue-backtotop": "^1.6.1", "vue-chartjs": "^3.4.2", "vue-class-component": "^7.2.3", "vue-clipboard2": "^0.3.0", "vue-context": "4.0.0", "vue-echarts": "^4.0.3", "vue-feather-icons": "^5.0.0", "vue-flatpickr-component": "^8.1.2", "vue-form-wizard": "^0.8.4", "vue-fullcalendar": "^1.0.9", "vue-i18n": "^8.11.2", "vue-instantsearch": "^2.2.1", "vue-perfect-scrollbar": "^0.1.0", "vue-prism-component": "^1.1.1", "vue-property-decorator": "^8.4.1", "vue-quill-editor": "^3.0.6", "vue-router": "^3.0.6", "vue-select": "^3.1.0", "vue-simple-calendar": "^4.2.2", "vue-simple-suggest": "^1.9.5", "vue-star-rating": "^1.6.1", "vue-tour": "^1.1.0", "vue-tree-halower": "^1.8.0", "vue-video-player": "^5.0.2", "vue2-google-maps": "^0.10.6", "vue2-hammer": "^2.1.2", "vuedraggable": "^2.21.0", "vuejs-datepicker": "^1.5.4", "vuesax": "3.11.1", "vuex": "^3.1.1", "xlsx": "^0.15.0" }, "devDependencies": { "@fullhuman/postcss-purgecss": "^1.2.0", "@types/jest": "^24.0.19", "@vue/cli-plugin-babel": "^3.7.0", "@vue/cli-plugin-typescript": "^4.3.1", "@vue/cli-plugin-unit-jest": "^4.3.1", "@vue/cli-service": "^3.7.0", "@vue/test-utils": "^1.0.0-beta.31", "axios-mock-adapter": "^1.17.0", "jest-localstorage-mock": "^2.4.2", "node-sass": "^4.12.0", "purgecss": "^1.3.0", "sass-loader": "^7.1.0", "script-loader": "0.7.2", "tailwindcss": "^1.0.1", "typescript": "^3.5.2", "vue-template-compiler": "^2.6.10" }, "jest": { "setupFiles": [ "jest-localstorage-mock" ], "preset": "@vue/cli-plugin-unit-jest/presets/typescript-and-babel" } }</pre> </div></vue<>

[英]Typescript Error: Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions<Vue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Typescript 錯誤:Object 字面量只能指定已知屬性,並且“組件選項”類型中不存在“路由器” <vue< div><div id="text_translate"><p> 我剛剛將 typescript 添加到我現有的 vuejs 應用程序中,我得到了一個Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt;'錯誤。 我的理解是我不需要安裝@types/vue 或@types/vue-router 來解決這個問題。 有人可以建議新手嗎?</p><p> <strong>~\src\router.js</strong></p><pre> import Vue from 'vue'; import Router from 'vue-router'; Vue.use(Router); const router = new Router({............ }); export default router;</pre><p> <strong>堆棧跟蹤:</strong></p><pre> ERROR in ~/src/main.ts(126,10): 126:10 No overload matches this call. Overload 1 of 3, '(options?: ThisTypedComponentOptionsWithArrayProps&lt;Vue, object, object, object, never&gt; | undefined): CombinedVueInstance&lt;Vue, object, object, object, Record&lt;...&gt;&gt;', gave the following error. Argument of type '{ router: any; store: any; i18n: any; acl: any; render: (h: CreateElement) =&gt; VNode; }' is not assignable to parameter of type 'ThisTypedComponentOptionsWithArrayProps&lt;Vue, object, object, object, never&gt;'. Object literal may only specify known properties, and 'router' does not exist in type 'ThisTypedComponentOptionsWithArrayProps&lt;Vue, object, object, object, never&gt;'. Overload 2 of 3, '(options?: ThisTypedComponentOptionsWithRecordProps&lt;Vue, object, object, object, object&gt; | undefined): CombinedVueInstance&lt;Vue, object, object, object, Record&lt;...&gt;&gt;', gave the following error. Argument of type '{ router: any; store: any; i18n: any; acl: any; render: (h: CreateElement) =&gt; VNode; }' is not assignable to parameter of type 'ThisTypedComponentOptionsWithRecordProps&lt;Vue, object, object, object, object&gt;'. Object literal may only specify known properties, and 'router' does not exist in type 'ThisTypedComponentOptionsWithRecordProps&lt;Vue, object, object, object, object&gt;'. Overload 3 of 3, '(options?: ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt; | undefined): CombinedVueInstance&lt;...&gt;', gave the following error. Argument of type '{ router: any; store: any; i18n: any; acl: any; render: (h: CreateElement) =&gt; VNode; }' is not assignable to parameter of type 'ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt;'. Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions&lt;Vue, DefaultData&lt;Vue&gt;, DefaultMethods&lt;Vue&gt;, DefaultComputed, PropsDefinition&lt;Record&lt;string, any&gt;&gt;, Record&lt;...&gt;&gt;'. 124 | 125 | Vue.config.productionTip = false; &gt; 126 | new Vue({router, store, i18n, acl, render: h =&gt; h(App)}).$mount('#app'); | ^ Version: typescript 3.8.3</pre><p> <strong>package.json:</strong></p><pre> { "name": "video-audio-transcription", "version": "4.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "test:unit": "vue-cli-service test:unit", "lint": "vue-cli-service lint" }, "dependencies": { "@aws-amplify/ui-vue": "^0.2.3", "@chenfengyuan/vue-countdown": "^1.1.2", "@react-native-community/netinfo": "^5.5.0", "@syncfusion/ej2-vue-grids": "^18.1.46", "@types/chart.js": "^2.9.20", "@types/vue2-hammer": "^2.1.0", "ag-grid-community": "^21.0.1", "ag-grid-vue": "^21.0.1", "algoliasearch": "^3.33.0", "apexcharts": "^3.6.12", "aws-amplify": "^3.0.9", "axios": "^0.19.0", "chart.js": "^2.8.0", "core-js": "2.6.5", "echarts": "^4.2.1", "file-saver": "2.0.1", "firebase": "^6.0.4", "instantsearch.css": "^7.3.1", "jsonwebtoken": "^8.5.1", "material-icons": "^0.3.1", "perfect-scrollbar": "^1.4.0", "postcss-rtl": "^1.5.0", "prismjs": "^1.16.0", "vee-validate": "^2.2.8", "vue": "^2.6.11", "vue-acl": "4.0.7", "vue-apexcharts": "^1.3.5", "vue-awesome-swiper": "^3.1.3", "vue-backtotop": "^1.6.1", "vue-chartjs": "^3.4.2", "vue-class-component": "^7.2.3", "vue-clipboard2": "^0.3.0", "vue-context": "4.0.0", "vue-echarts": "^4.0.3", "vue-feather-icons": "^5.0.0", "vue-flatpickr-component": "^8.1.2", "vue-form-wizard": "^0.8.4", "vue-fullcalendar": "^1.0.9", "vue-i18n": "^8.11.2", "vue-instantsearch": "^2.2.1", "vue-perfect-scrollbar": "^0.1.0", "vue-prism-component": "^1.1.1", "vue-property-decorator": "^8.4.1", "vue-quill-editor": "^3.0.6", "vue-router": "^3.0.6", "vue-select": "^3.1.0", "vue-simple-calendar": "^4.2.2", "vue-simple-suggest": "^1.9.5", "vue-star-rating": "^1.6.1", "vue-tour": "^1.1.0", "vue-tree-halower": "^1.8.0", "vue-video-player": "^5.0.2", "vue2-google-maps": "^0.10.6", "vue2-hammer": "^2.1.2", "vuedraggable": "^2.21.0", "vuejs-datepicker": "^1.5.4", "vuesax": "3.11.1", "vuex": "^3.1.1", "xlsx": "^0.15.0" }, "devDependencies": { "@fullhuman/postcss-purgecss": "^1.2.0", "@types/jest": "^24.0.19", "@vue/cli-plugin-babel": "^3.7.0", "@vue/cli-plugin-typescript": "^4.3.1", "@vue/cli-plugin-unit-jest": "^4.3.1", "@vue/cli-service": "^3.7.0", "@vue/test-utils": "^1.0.0-beta.31", "axios-mock-adapter": "^1.17.0", "jest-localstorage-mock": "^2.4.2", "node-sass": "^4.12.0", "purgecss": "^1.3.0", "sass-loader": "^7.1.0", "script-loader": "0.7.2", "tailwindcss": "^1.0.1", "typescript": "^3.5.2", "vue-template-compiler": "^2.6.10" }, "jest": { "setupFiles": [ "jest-localstorage-mock" ], "preset": "@vue/cli-plugin-unit-jest/presets/typescript-and-babel" } }</pre> </div></vue<> 打字稿“對象文字可能只指定已知屬性”對 Promise 返回函數沒有影響 Mobx - 對象字面量只能指定已知屬性 對象字面量只能指定已知的屬性,並且類型“設置”中不存在“選擇” Typescript - 如何聲明具有已知屬性的匿名對象? 如何在typescript定義中指定松散類型的對象文字? 無法使用打字稿向對象文字添加新屬性 為什么我可以將未知屬性分配給 typescript 中的文字 object? TypeScript錯誤對象原型只能是一個對象或null:未定義 具有已知屬性的 Typescript 對象
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM