From 98fa6fd0e7d6c6f300c43b58d0ce7020f4eea5fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Ba=CC=88chler?= Date: Tue, 23 Sep 2025 09:34:07 +0200 Subject: [PATCH 1/3] fix: Use readonly type for languages array. Defining the array argument as readonly allows setting the language array as const. Regular arrays are still allowed as `languages` param. The `readonly` is only applied to within the languages function. Defining the language array as const makes typescript aware of the available languages. --- Library/Typings.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Typings.d.ts b/Library/Typings.d.ts index 3d457b1..875844e 100644 --- a/Library/Typings.d.ts +++ b/Library/Typings.d.ts @@ -6,7 +6,7 @@ declare module 'accept-language' { /** * Define your supported languages. The first language will be your default language. */ - languages(languages: string[]): void; + languages(languages: readonly string[]): void; /** * Get matched language. If no match, the default language will be returned. From 29e08a3d05dc00b6f7d44c081b09fec94d213987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=C3=A4chler?= <288516+sbaechler@users.noreply.github.com> Date: Mon, 29 Sep 2025 21:25:46 +0200 Subject: [PATCH 2/3] Update Library/Typings.d.ts Co-authored-by: MiroslavPetrik --- Library/Typings.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Typings.d.ts b/Library/Typings.d.ts index 875844e..e6c7ee7 100644 --- a/Library/Typings.d.ts +++ b/Library/Typings.d.ts @@ -6,7 +6,7 @@ declare module 'accept-language' { /** * Define your supported languages. The first language will be your default language. */ - languages(languages: readonly string[]): void; + languages(languages: readonly [string, ...string[]]): void; /** * Get matched language. If no match, the default language will be returned. From 56e0eb6b631a665f2dd257fd52a85f1b7aec6798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Ba=CC=88chler?= Date: Mon, 29 Sep 2025 21:37:19 +0200 Subject: [PATCH 3/3] Add correct types to Source as well --- Source/AcceptLanguage.ts | 2 +- Tests/Test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/AcceptLanguage.ts b/Source/AcceptLanguage.ts index 9fb813d..690bc54 100644 --- a/Source/AcceptLanguage.ts +++ b/Source/AcceptLanguage.ts @@ -18,7 +18,7 @@ class AcceptLanguage { private defaultLanguageTag: string | null = null; - public languages(definedLanguages: string[]) { + public languages(definedLanguages: readonly [string, ...string[]]) { if (definedLanguages.length < 1) { throw new Error('No language tags defined. Provide at least 1 language tag to match.'); } diff --git a/Tests/Test.ts b/Tests/Test.ts index 0c7de2d..493f0d5 100644 --- a/Tests/Test.ts +++ b/Tests/Test.ts @@ -5,7 +5,7 @@ import AcceptLanguage from '../Source/AcceptLanguage'; import chai = require('chai'); const expect = chai.expect; -function createInstance(definedLanguages?: string[]) { +function createInstance(definedLanguages?: readonly [string, ...string[]]) { const al = AcceptLanguage.create(); if (definedLanguages) { al.languages(definedLanguages); @@ -16,7 +16,7 @@ function createInstance(definedLanguages?: string[]) { describe('Language definitions', () => { it('should throw when defined languages is empty', () => { const method = () => { - createInstance([]); + createInstance([] as any); }; expect(method).to.throw(); });