আমি কীভাবে টাইপস্ক্রিপ্টের সাথে অবজেক্টগুলির অ্যারের জন্য একটি ইন্টারফেস সংজ্ঞায়িত করতে পারি?


124

আমি নিম্নলিখিত ইন্টারফেস এবং কোড আছে। আমি ভেবেছিলাম আমি সংজ্ঞাগুলি সঠিকভাবে করছি তবে আমি একটি ত্রুটি পাচ্ছি:

interface IenumServiceGetOrderBy { id: number; label: string; key: any }[];

এবং:

getOrderBy = (entity): IenumServiceGetOrderBy => {
        var result: IenumServiceGetOrderBy;
        switch (entity) {
            case "content":
                result =
                [
                    { id: 0, label: 'CId', key: 'contentId' },
                    { id: 1, label: 'Modified By', key: 'modifiedBy' },
                    { id: 2, label: 'Modified Date', key: 'modified' },
                    { id: 3, label: 'Status', key: 'contentStatusId' },
                    { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
                    { id: 5, label: 'Title', key: 'title' },
                    { id: 6, label: 'Type', key: 'contentTypeId' },
                    { id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
                ];
                break;
        }
        return result;
    };

ত্রুটি:

Error   190 Cannot convert '{}[]' to 'IenumServiceGetOrderBy':
    Type '{}[]' is missing property 'id' from type 'IenumServiceGetOrderBy'

উত্তর:


104

আপনি একটি সঙ্গে একটি ইন্টারফেস বর্ণনা করতে পারেন indexer :

interface EnumServiceGetOrderBy {
    [index: number]: { id: number; label: string; key: any };
}

37
সত্যই কোনও "টাইপসক্রিপ্টযুক্ত বস্তুর অ্যারের জন্য ইন্টারফেস" নয়
— বাসারাত

হ্যাঁ, নির্দিষ্ট আইটেমগুলির জন্য আপনার ইন্টারফেসটি সংজ্ঞায়িত করার উদাহরণটি এটি করার জন্য আরও কার্যকর উপায় হবে। আইটেমগুলির একটি অ্যারে পাওয়া বেশ বিরল হবে তবে কোনও একক আইটেমকে সুবিধামত উল্লেখ করতে চান না। সত্যিকারের অ্যারে ব্যবহার করা ইন্টারফেসে। দৈর্ঘ্য প্রকাশ করে, যা সম্ভবত বেশিরভাগ ক্ষেত্রে ব্যবহৃত হবে।
— ডগলাস

এই ইন্টারফেসটি ইমপ্লিমেন্ট করতে ক্লাস কীভাবে সংজ্ঞায়িত করবেন?
— অ্যালেক্সি

20
এই সমাধান সঙ্গে সমস্যা এই একটি নয় Arrayইন্টারফেস সেইজন্য সমস্ত, Arrayবৈশিষ্ট্য এবং যেমন পদ্ধতি lengthবা pushঅনুপস্থিত করা হবে না।
— নরম্যান ব্রিউ

232

আপনার একটি সূচক ব্যবহার করার প্রয়োজন নেই (যেহেতু এটি কিছুটা কম টাইপসেজের)। আপনার দুটি বিকল্প রয়েছে:

interface EnumServiceItem {
    id: number; label: string; key: any
}

interface EnumServiceItems extends Array<EnumServiceItem>{}


// Option A 
var result: EnumServiceItem[] = [
    { id: 0, label: 'CId', key: 'contentId' },
    { id: 1, label: 'Modified By', key: 'modifiedBy' },
    { id: 2, label: 'Modified Date', key: 'modified' },
    { id: 3, label: 'Status', key: 'contentStatusId' },
    { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
    { id: 5, label: 'Title', key: 'title' },
    { id: 6, label: 'Type', key: 'contentTypeId' },
    { id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
];

// Option B
var result: EnumServiceItems = [
    { id: 0, label: 'CId', key: 'contentId' },
    { id: 1, label: 'Modified By', key: 'modifiedBy' },
    { id: 2, label: 'Modified Date', key: 'modified' },
    { id: 3, label: 'Status', key: 'contentStatusId' },
    { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
    { id: 5, label: 'Title', key: 'title' },
    { id: 6, label: 'Type', key: 'contentTypeId' },
    { id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
]

ব্যক্তিগতভাবে আমি অপশন এ এর ​​সুপারিশ করি (আপনি যখন ইন্টারফেস নয় ক্লাস ব্যবহার করছেন তখন সহজ স্থানান্তর)।


4
?ইন্টারফেসের সমস্ত ঘোষণায় যুক্ত করা হচ্ছে, যেমন। id?: number; label?: string; key?: anyআপনি যদি ইন্টারফেসে সংজ্ঞায়িত হওয়ার চেয়ে কম সম্পত্তি ব্যবহার করেন তবে কাস্টিং ইস্যু এড়ানোর সর্বোত্তম উপায় ? অর্থাৎ। keyটাইপ করা অ্যারে সেটআপে আমি উল্লেখ করতে পারি না ।
— গোনিলে

বিকল্প বিটি কার্যকর কারণ এটি উচ্চ স্তরের ইন্টারফেস রচনা করার সময় আপনাকে ইন্টারফেসটি ব্যবহার করতে দেয়।
— অশটোনক

4
গীজ খ্রিস্ট এই এতদিন আমাকে বিরক্ত করেছেন, ধন্যবাদ আপনাকে ধন্যবাদ!
— ম্যাথু হারউড

4
দ্বিতীয় ইন্টারফেস ঘোষণা নিম্নলিখিত tslint ত্রুটি দেয়: " কোনো সদস্য প্রকাশক একটি ইন্টারফেস তার supertype সমতূল্য "
— Inigo

4
আমি কি সুপারিশ করতে পারি যে 'কী' টাইপ স্ট্রিংয়ের | স্ট্রিং [] এর পরিবর্তে কোনও। এটি পছন্দ করুন:interface EnumServiceItem { id: number; label: string; key: string | string[] }
— জুলিয়ানজি

84

আপনি কেবল অ্যারে ইন্টারফেসটি প্রসারিত করে একটি ইন্টারফেসটিকে অ্যারে হিসাবে সংজ্ঞায়িত করতে পারেন।

export interface MyInterface extends Array<MyType> { }

এটির সাহায্যে যে কোনও বস্তু যা প্রয়োগ করে MyInterfaceতার জন্য অ্যারের সমস্ত ফাংশন কলগুলি প্রয়োগ করা প্রয়োজন এবং কেবলমাত্র এটির সাহায্যে অবজেক্টগুলি সঞ্চয় করতে সক্ষম হবেMyType টাইপের ।


7
সুন্দর সমাধান। এটি গ্রহণযোগ্য উত্তর হওয়া উচিত।
— demisx

4
type MyTypeArray = Array<MyType>ইন্টারফেসটি বাড়ানোর পরিবর্তে কোনও প্রকার ( ) ব্যবহার করবেন না কেন ?
— পাউ ফ্রেস 10

ওপি ইন্টারফেসের জন্য জিজ্ঞাসা করেছিল, আমি ধরে নিয়েছি এটি অন্যান্য বৈশিষ্ট্য এবং পদ্ধতিগুলির সাথে প্রসারিত হয়েছে, আপনি কোনও প্রকারের সাহায্যে এটি করতে পারবেন না এবং
— টাইপও

@ NoNameProvided "প্রকারটি সাধারণ ব্যবহারের ক্ষেত্রে শর্তাদির চেয়ে ইন্টারফেসের চেয়ে বেশি সীমাবদ্ধ" আপনি কি এই বিবৃতিতে আরও বিশদ সরবরাহ করতে পারবেন? ধরণের বনাম ইন্টারফেস ব্যবহারের সীমাবদ্ধতাগুলি কী?
— পাউ ফ্রেস 11

4
অন্যের চেয়ে সেরা সমাধানটির গ্রহণযোগ্য উত্তর হওয়া উচিত
— অ্যান্ড্রেস ফিলিপ


9

ব্যবহার করবেন না

interface EnumServiceGetOrderBy {
    [index: number]: { id: number; label: string; key: any };
}

আপনি সমস্ত অ্যারে বৈশিষ্ট্য এবং পদ্ধতি যেমন স্প্লাইস ইত্যাদির জন্য ত্রুটি পাবেন

সমাধানটি এমন একটি ইন্টারফেস তৈরি করা যা অন্য ইন্টারফেসের অ্যারে সংজ্ঞা দেয় (যা বস্তুর সংজ্ঞা দেবে)

উদাহরণ স্বরূপ:

interface TopCategoriesProps {
  data: Array<Type>;
}
interface Type {
  category: string;
  percentage: number;
}

5

কৌণিক ক্ষেত্রে অবজেক্টের একটি 'অ্যারে' এর জন্য ইন্টারফেস সংজ্ঞায়িত করতে 'প্রসারিত' ব্যবহার করুন।

একটি সূচক ব্যবহার করা আপনাকে ত্রুটি দেয় কারণ এটি অ্যারে ইন্টারফেস নয় তাই বৈশিষ্ট্য এবং পদ্ধতিগুলি অন্তর্ভুক্ত করে না।

যেমন

ত্রুটি TS2339: সম্পত্তি 'খুঁজে পাওয়া' 'ISelectOptions2' টাইপটিতে বিদ্যমান নেই।

// good    
export interface ISelectOptions1 extends Array<ISelectOption> {}

// bad
export interface ISelectOptions2 {
    [index: number]: ISelectOption;
}

interface ISelectOption {
    prop1: string;
    prop2?: boolean;
}

যদি এই লিটারটি খালি ইন্টারফেসের অনুমতি না দেয় তবে এই পদ্ধতির ফলে "কোনও সদস্যের ঘোষিত একটি ইন্টারফেস তার সুপারটাইপের সমতুল্য হয়" এর ফলস্বরূপ।
— ডেমিম্যাক্স

এই উত্তরটি খুব কাছাকাছি ছিল এটি আমার সমস্যা সমাধানে সহায়তা করেছিল! অন্য যে কোনও ইন্টারফেসের মধ্যে আপনি যে অ্যারে তৈরি করতে চান সেই প্রাথমিক অবজেক্টের জন্য, আপনি "ইন্টারফেস ISelectOptions {name1: টাইপ; নাম 2: টাইপ; type টাইপ করুন B তারপরে আপনার বড় ইন্টারফেসের মধ্যে, আপনি কীটি পেতে চাইলে একটি অ্যারে হতে চান এই ধরণের, keyZ: ISelectOptions []; তারপরে আপনি JSON তৈরি করার সময়, myVar: BIG = {key1: val1, key2: val2,…, keyZ: [{name1: valA, name2: valB}, {name1: valC, name2 : valD}] চ্যাম্পের মতো কাজ করেছিল! (যদিও অ্যারে ক্লাসটি বাড়ানোর দরকার ছিল না) ধন্যবাদ!
— অ্যাপড্রেমার

4

আপনি যদি একটি সম্পূর্ণ নতুন ধরণের তৈরি করতে না চান তবে এখানে একটি ইনলাইন সংস্করণ রয়েছে:

export interface ISomeInterface extends Array<{
    [someindex: string]: number;
}> { };

4

এছাড়াও আপনি এটি করতে পারেন।

            interface IenumServiceGetOrderBy {
                id: number; 
                label: string; 
                key: any;
            }

            // notice i am not using the []
            var oneResult: IenumServiceGetOrderBy = { id: 0, label: 'CId', key: 'contentId'};

            //notice i am using []
            // it is read like "array of IenumServiceGetOrderBy"
            var ArrayOfResult: IenumServiceGetOrderBy[] = 
            [
                { id: 0, label: 'CId', key: 'contentId' },
                { id: 1, label: 'Modified By', key: 'modifiedBy' },
                { id: 2, label: 'Modified Date', key: 'modified' },
                { id: 3, label: 'Status', key: 'contentStatusId' },
                { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
                { id: 5, label: 'Title', key: 'title' },
                { id: 6, label: 'Type', key: 'contentTypeId' },
                { id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
            ];

3

কোনও tslint ত্রুটি সহ সহজ বিকল্প ...

export interface MyItem {
    id: number
    name: string
}

export type MyItemList = [MyItem]

5
রফতানি করা MyItemListMyItem
— ধরণটি

5
এটা হতে পারে export type MyItemList = MyItem[]? আইএমও একটি ইন্টারফেস এবং একটি টাইপ ওরফে সংমিশ্রণ এই ক্ষেত্রে সবচেয়ে উপযুক্ত কারণ আপনার টাইপের
— উপন্যাসে

3

আপনার উদাহরণের সাথে অভিযোজিত একটি সমাধান এখানে দেওয়া হয়েছে:

interface IenumServiceGetOrderByAttributes { 
  id: number; 
  label: string; 
  key: any 
}

interface IenumServiceGetOrderBy extends Array<IenumServiceGetOrderByAttributes> {

}

let result: IenumServiceGetOrderBy;

এই সমাধানের সাহায্যে আপনি অ্যারের সমস্ত বৈশিষ্ট্য এবং পদ্ধতি ব্যবহার করতে পারেন (যেমন: length, push(), pop(), splice()...)


3

এভাবে ব্যবহার করুন!

interface Iinput {
  label: string
  placeholder: string
  register: any
  type?: string
  required: boolean
}


// This is how it can be done

const inputs: Array<Iinput> = [
  {
    label: "Title",
    placeholder: "Bought something",
    register: register,
    required: true,
  },
]

2

প্রোগ্রামিং সহজ। সাধারণ ইউজকেস ব্যবহার করুন:

interface IenumServiceGetOrderBy { id: number; label: string; key: any }
 // OR
interface IenumServiceGetOrderBy { id: number; label: string; key: string | string[] }

// use interface like
const result: IenumServiceGetOrderBy[] = 
                [
                    { id: 0, label: 'CId', key: 'contentId' },
                    { id: 1, label: 'Modified By', key: 'modifiedBy' },
                    { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] }
                ];


আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.